Your error was here href\=\"(.*)
too greedy... replaced with href\=\"(.*?)
preg_match_all('%\[link.*?href="(.*?)?".*?title="(.*?)".*?class="(.*?)".*?\](.*?)\[\/link\]%i', $email, $result, PREG_PATTERN_ORDER);
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?
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);