问题描述:

I'm looking to strip all but the Flickr photo page URL from a block of BBCode, using the following as an example:

[img]http://farm8.staticflickr.com/7025/6821895917_a372a271a2_z.jpg[/img]

Y159 being sanded at Daylesford by VRfan, on Flickr

All I want is the value of the first [url] tag (http://www.flickr.com/photos/vrfan/6821895917/) - everything else can be removed.

Hopefully a regex wizard can help me out on this :)

Cheers,

Michael.

网友答案:

You can use substr() too combined with strpos() instead of Regex.

$matches = array();
$f = strpos($str, '[url=');
while ($f)
{
    $end = strpos($str, ']', $f);
    $f += 5;
    $matches[] = substr($str, $f, $end - $f);
    $f = strpos($str, '[url=', $end);
}
var_dump($matches);
// Regex Solution
preg_match_all('/\[url=(.+?)\]/i', $str, $matches);
var_dump($matches[1]);
相关阅读:
Top