我有3个版本的字符串:
[center][thumb]http://some_domain.com/uploads/posts/2010-04/1271272006_tn.jpg[/thumb][/center]
[center][img]http://some_domain.com/uploads/posts/2012-01/1325796885.jpg[/img][/center]
[img]http://some_domain.com/uploads/posts/2012-01/1325796885.jpg[/img]
它也可以是[left][/left]
或[right][/right]
。前两个我通过/\[(center|left|right)\]\[(img|thumb)\](.*)?\[(\/img|\/thumb)\]\[(\/center|\/left|\/right)\]/
选择,但第三个是一个问题:如何检查“之前”是否不存在?
P.S。 我只需要获取网址。
答案 0 :(得分:2)
为什么不删除那些“标签”?
$buffer = strtr($input, array('[' => '<', ']' => '>'));
$url = strip_tags($buffer);
对于你的三个例子,这是:
http://some_domain.com/uploads/posts/2010-04/1271272006_tn.jpg
http://some_domain.com/uploads/posts/2012-01/1325796885.jpg
http://some_domain.com/uploads/posts/2012-01/1325796885.jpg
答案 1 :(得分:0)
更简单,更不优雅:
$str = '[center][img]http://some_domain.com/uploads/posts/2012-01/1325796885.jpg[/img][/center]';
$url = preg_replace('/.*?(http.+?)\[\/.*+/', "$1", $str);
答案 2 :(得分:0)
如果您要删除所有[*]
“代码”,可以执行以下操作:
$tag_replace_pattern = '#\[.*\]#U'; // note 'U' pattern modifier for ungreedy search
$url = preg_replace($tag_replace_pattern, '', $original_string);
如果您只需要删除特定模式,可以使用:
$tag_replace_pattern = '#\[/?(thumb|left|center|right|img)\]#U';
$url = preg_replace($tag_replace_pattern, '', $original_string);