regular expression help

Discussion in 'PHP' started by imvain2, Apr 25, 2008.

  1. #1
    I'm new to regular expressions and I would appreciate some help.

    if the original code is something like:
    
     
    here is html code
     
    <font face="times new roman" size="+2"><B>Some Name Here</b></font>
     
    more html code
     
    
    HTML:
    and I want to extract just: Some Name Here

    I started with this code but it doesn't work:
    
        preg_match("<%font%[^>]*>(.*?)</%font%>", $pagecontent,$matches);
    
    PHP:
     
    imvain2, Apr 25, 2008 IP
  2. CPURules

    CPURules Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    function GSB($oString, $oStart, $oEnd, $start = 1) {
    $strings = explode($oStart, $oString);
    $tempstr = $strings[$start];
    
    $strings = explode($oEnd, $tempstr);
    return $strings[0];
    }
    
    PHP:
    GetStringBetween function, written by me.

    For your example, you would want to use this:
    
    <?php
    function GSB($oString, $oStart, $oEnd, $start = 1) {
    $strings = explode($oStart, $oString);
    $tempstr = $strings[$start];
    
    $strings = explode($oEnd, $tempstr);
    return $strings[0];
    }
    
    $html = "<font face=\"times new roman\" size=\"+2\"><B>Some Name Here</b></font>";
    
    $strName = GSB($html, 'size="+2"><B>', '</b></font>');
    
    echo $strName;
    ?>
    
    PHP:
    This would output 'Some Name Here'
     
    CPURules, Apr 25, 2008 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    
    preg_match("/<font face="times new roman" size="\+2"><B>(.*)<\/b><\/font>/U", $pagecontent,$matches);
    
    Code (markup):
    Should work.

    Peace,
     
    Barti1987, Apr 25, 2008 IP
    imvain2 likes this.
  4. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    azizny: Perfect thanks!!
     
    imvain2, Apr 25, 2008 IP