Kin
Kin

Reputation: 4596

How to check nothing in regexp

I have 3 versions of string:

  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]

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

Answers (3)

Mike Brant
Mike Brant

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

Madbreaks
Madbreaks

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

hakre
hakre

Reputation: 197684

Why not just remove those "tags"?

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

See strtrDocs and strip_tagsDocs

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

Related Questions