Php 如何从包含标记的字符串中提取URL?

Php 如何从包含标记的字符串中提取URL?,php,regex,Php,Regex,正在尝试获取图像数组(image1.jpg、image2.gif、image3.png)。图像可能介于[img][\img]和[thumb][\thumb]之间。我还需要检查一整行[center][thumb]http://domain.com/image3.png[/thumb][/center]使用其他图像更改它。有人能帮忙吗 下面的模式将其全部提取出来 $string = '[url=http://domain.com]My Webpage[/url][img]http://domain.

正在尝试获取图像数组(image1.jpg、image2.gif、image3.png)。图像可能介于
[img][\img]
[thumb][\thumb]
之间。我还需要检查一整行
[center][thumb]http://domain.com/image3.png[/thumb][/center]
使用其他图像更改它。有人能帮忙吗

下面的模式将其全部提取出来

$string = '[url=http://domain.com]My Webpage[/url][img]http://domain.com/image1.jpg[/img][center][img]http://domain.com/image2.gif[/img][/center][center][thumb]http://domain.com/image3.png[/thumb][/center]';
preg_match_all('/.*?\[img|thumb\](http.+?)\[\/.*+/', $string, $matches);
var_dump($matches);

您不应该尝试使用正则表达式进行解析。使用循环从一个
strpos()
结果迭代到下一个。会更快更扎实地工作。
@\[(img|thumb)\](.+?)\[/\\1@