How to retrieve a patterned string from a paragraph

Discussion in 'PHP' started by weilies, Jan 2, 2010.

  1. #1
    hi gurus,

    i have a string
    how can i retrieve the text
    http://img215.imageshack.us/img215/1886/cleard.jpg

    Thanks for the guide
     
    weilies, Jan 2, 2010 IP
  2. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Your code would be something like this:

    <?php
    $regex = ''; // REGEX HERE
    $string = 'some text...
    [img:304uninl]http://img215.imageshack.us/img215/1886/cleard.jpg[/img:304uninl]
    some text... ';
    
    preg_match($regex, $string, $matches);
    
    print_r($matches);
    PHP:
    Unfortunately, I do not understand regular expressions so someone will need to pick up where i left off.
     
    CodedCaffeine, Jan 2, 2010 IP
  3. shubhamjain

    shubhamjain Active Member

    Messages:
    215
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #3
    $regex = '%\](^\[)+\[';

    I am not sure BTW. :D
     
    shubhamjain, Jan 2, 2010 IP
  4. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hah, you got farther than I did. This is what it came up with:

     
    CodedCaffeine, Jan 2, 2010 IP
  5. shubhamjain

    shubhamjain Active Member

    Messages:
    215
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #5
    oh sorry it is...

    $regex = '%\](^\[)+\[%'; :D
     
    shubhamjain, Jan 2, 2010 IP
  6. weilies

    weilies Peon

    Messages:
    453
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i have run my code
    <?php
    
    $regex = '%\](^\[)+\[%'; // REGEX HERE
    $string = 'some text...
    [img:304uninl]http://img215.imageshack.us/img215/1886/cleard.jpg[/img:304uninl]
    some text... ';
    
    preg_match($regex, $string, $matches);
    
    print_r($matches);
    ?>
    PHP:
    return NULL
     
    weilies, Jan 2, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    <?php
    
    $string = <<<STRING
    some text...
    [img:304uninl]http://img215.imageshack.us/img215/1886/cleard.jpg[/img:304uninl]
    some text... 
    STRING;
    
    $text = explode("\n", $string);
    
    //the first text
    print($text[0]); 
    
    //the second lot of text
    print($text[2]);
    
    ?>
    PHP:
     
    danx10, Jan 2, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    You should use the 's' modifier, since the text being match'd contains new lines.
     
    danx10, Jan 2, 2010 IP
  9. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #9
    $str = "[img:304uninl]http://img215.imageshack.us/img215/1886/cleard.jpg[/img:304uninl]";
    $exp =  explde("uninl]",$str); // explode the start point
    $pos = strpos($exp[1],"[/"); // position the end point
    $sub = substr($exp[1],0,$pos); // cut the link
    echo $sub;
    PHP:
    If there are more links on page then

    
    $str = "[img:304uninl]http://img215.imageshack.us/img215/1886/cleard.jpg[/img:304uninl]";
    foreach(range(1,20) as $range) { // from,to array values
    $exp =  explde("uninl]",$str); // explode the start points
    $pos = strpos($exp[$range],"[/"); // get position of end points
    $sub = substr($exp[$range],0,$pos); // cut the link
    echo $sub; echo "<br>"; }
    PHP:
     
    iAreCow, Jan 5, 2010 IP