2

我有 3 个版本的字符串:

  1. [center][thumb]http://some_domain.com/uploads/posts/2010-04/1271272006_tn.jpg[/thumb][/center]
  2. [center][img]http://some_domain.com/uploads/posts/2012-01/1325796885.jpg[/img][/center]
  3. [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)\]/,但第三个是一个问题:如何检查“以前的”是否不存在?

PS我只需要获取网址。

4

3 回答 3

2

为什么不直接删除那些“标签”?

$buffer = strtr($input, array('[' => '<', ']' => '>'));
$url = strip_tags($buffer);

查看strtr文档strip_tags文档

对于您的三个示例,这是:

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
于 2013-01-08T00:11:24.307 回答
0

一些更简单且不那么优雅的东西:

$str = '[center][img]http://some_domain.com/uploads/posts/2012-01/1325796885.jpg[/img][/center]';

$url = preg_replace('/.*?(http.+?)\[\/.*+/', "$1", $str);
于 2013-01-08T00:11:32.067 回答
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);
于 2013-01-08T00:20:44.907 回答