Question

I have this regex which works fine only if the strings are on multiline

#\[link.*?href\=\"(.*)?\".*?title\=\"(.*?)\".*?class\=\"(.*?)\".*?\](.*?)\[\/link\]#e

matches ok if second string is on new line

[link href="somehref" title="sometitle" class="someclas"]Text[/link]
[link href="somehref" title="sometitle" class="someclas"]Text2[/link]

matches last one partially if everything is on single line

[link href="somehref" title="sometitle" class="someclas"]Text[/link][link href="somehref" title="sometitle" class="someclas"]Text2[/link]

How do I make it match either, multi or single line?

Was it helpful?

Solution

Your error was here href\=\"(.*) too greedy... replaced with href\=\"(.*?)

preg_match_all('%\[link.*?href="(.*?)?".*?title="(.*?)".*?class="(.*?)".*?\](.*?)\[\/link\]%i', $email, $result, PREG_PATTERN_ORDER);

http://regex101.com/r/lG7sQ6

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top