1

i have:

<a href="http://www.test.com/performers/test-1/" title="">test 1</a>                                <a href="http://www.test.com/performers/test-2/" title="">test 2</a>

I would like title 1,title 2 as result.

I wrote:

$m=array();
preg_match_all('/<a href="http:\/\/www.test.com\/performers\/(.*)\/" title="">(.*)<\/a>/i', $buff,$m);
$info['models'] = implode(',', $m[2]);

I get only title 2 as result.

But if title 1 and title 2 html codes are not on the same line, i get title 1,title 2.

How to get title 1,title 2 as result when all the html code is on the same line?

Thank you.

1

1 Answer 1

0

Problem is greedyness of regex. The (.)* part in your regex matches test-1/" title="">test 1</a> <a href="http://www.test.com/performers/test-2 instead of only test-1. If you add the modifyier U to your pattern your regex works. But you should escape some more characters in your regexp, i.e. all dots match any character, not only a dot:

$m = array();
preg_match_all('/<a\ href="http:\/\/www\.test\.com\/performers\/(.*)\/"\ title="">(.*)<\/a>/Ui', $buff,$m);
$info['models'] = implode(',', $m[2]);

And, to be fair, working on DOM would be much safer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.