Need help with preg_match

Discussion in 'PHP' started by Crayz, Feb 9, 2007.

  1. #1
    Hello, i'v looked it up on php.net but was a little confused, so I gotta ask around :)

    Say I had a text file containing a code such as

    <b>texttexttexttext</b><i123>[B]clay[/B]</i><br><u>ping</u>
    Code (markup):
    I want to get the data between the <i123> and </i> tags, excluding the <i123> and </i>, and store it in a variable called $data, so when I put the code:

    echo "$data"; 
    Code (markup):
    the text clay will be displayed.

    Thanks!
     
    Crayz, Feb 9, 2007 IP
  2. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #2
    Well I put this together real quick, it doesnt use preg_match, but you can try it and give it a go:

    
    <?php
    $page=file_get_contents("file.htm"); // change to correct file name
    eregi("<i123>(.*)</i>", $page, $result);
    $data=$result[1];
    echo $data;
    ?> 
    
    PHP:
     
    smatts9, Feb 9, 2007 IP
  3. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #3
    One problem, when I try to get the content from a page from another site (i'm using a myspace url) it grabs all of the content of the myspace page and puts it into the variable too. Any suggestions?

    Edit: Fixed it, thank you, smatts9!
     
    Crayz, Feb 9, 2007 IP
  4. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php
    preg_match('#<i123>([a-z0-9-]+)</i>#si', $contents, $data);
    echo $data[1];
    ?>
    PHP:
     
    -NB-, Feb 10, 2007 IP
  5. wenzlerpaul

    wenzlerpaul Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    <?
    $string = '<b>texttexttexttext</b><i123>clay</i><br><u>ping</u>';
    
    preg_match('/<i123>([^<]*)<\/i>/', $string, $match);
    
    echo $match[1];
    
    //or 
    
    $data = $match[1];
    
    echo $data;
    ?>
    
    PHP:
     
    wenzlerpaul, Feb 12, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Note that a-z0-9- can be simplified by \w. (Only thing, \w includes underscores as well.)
     
    nico_swd, Feb 12, 2007 IP
  7. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks nico_swd, didn't know that :)
     
    -NB-, Feb 15, 2007 IP