-1

I would like to have for each a href the href value and add it to an array.

This is my code so far:

$str = "<a href='google.com'>ga naar google</a> <a href='nu.nl'>of ga naar nu.nl</a>";
preg_match_all('/\<a href="{1}([^">]*)\">{1}/', $str, $array);
print_r($array[1]);

I tried to do it with preg_match_all and add the values to an array called 'array'. I tried multiple times but didn't get the values that i wanted.

P. Kumar
  • 207
  • 1
  • 7

1 Answers1

2

You forgot about the difference between double and single quotes. This is the code that is working for your example. Not only the single quotes were wrong, also some other things like the slashes in there.

$str = "<a href='google.com'>ga naar google</a> <a href='nu.nl'>of ga naar nu.nl</a>";
preg_match_all('<a href=[\'\"]([^\"\']*)[\"\']>', $str, $array);
print_r($array[1]);

If you want to see the result online: http://sandbox.onlinephpfunctions.com/code/67bcd18666c3673ee198615483614970975d980e

ndsmyter
  • 6,535
  • 3
  • 22
  • 37