Search code examples
phpregexpreg-match-allphpbb

Get only image url from phpbb post text


Since PHPBB have its own way of calling the inline images, i have post_text something like this

test content [img:32acu135]http://mywebsite.com.pk/wp-content/uploads/2012/10/some-thing-3.jpg[/img:32acu135]

in database, is there any way that i can call the first image in the content, may be using preg_match_all or regex..


Solution

  • I would go for a regex like

    \[img.+?\](.*)\[\/img.+?\]
    

    and take the capturing group

    php example

    $re = "/\\[img.+?\\](.*)\\[\\/img.+?\\]/"; 
    $str = "[img:32acu135]http://mywebsite.com.pk/wp-content/uploads/2012/10/some-thing-3.jpg[/img:32acu135]"; 
    
    preg_match($re, $str, $matches);
    
    print($matches[1]);
    

    Result :

    http://mywebsite.com.pk/wp-content/uploads/2012/10/some-thing-3.jpg