Reputation: 4596
I have 3 versions of string:
[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]
It also can be [left][/left]
or [right][/right]
. First two i selected via /\[(center|left|right)\]\[(img|thumb)\](.*)?\[(\/img|\/thumb)\]\[(\/center|\/left|\/right)\]/
, but with third is one problem: how to check if "previous" just don't exist ?
P.S. I need to get only url.
Upvotes: 2
Views: 78
Reputation: 71384
If you are trying to remove all [*]
"tags", you can do something like this:
$tag_replace_pattern = '#\[.*\]#U'; // note 'U' pattern modifier for ungreedy search
$url = preg_replace($tag_replace_pattern, '', $original_string);
If you only need to remove specific patterns you can use this:
$tag_replace_pattern = '#\[/?(thumb|left|center|right|img)\]#U';
$url = preg_replace($tag_replace_pattern, '', $original_string);
Upvotes: 0
Reputation: 19539
Something simpler and less elegant:
$str = '[center][img]http://some_domain.com/uploads/posts/2012-01/1325796885.jpg[/img][/center]';
$url = preg_replace('/.*?(http.+?)\[\/.*+/', "$1", $str);
Upvotes: 0
Reputation: 197684
Why not just remove those "tags"?
$buffer = strtr($input, array('[' => '<', ']' => '>'));
$url = strip_tags($buffer);
See strtr
Docs and strip_tags
Docs
For your three examples this is:
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
Upvotes: 2