Greedy selector
The problem here is that your selector is greedy - it will match the longest possible string it can:
preg_match('/href="http:\/\/(.+)">/', $a, $b);
Since you use "any character" (.
) it is matching right up until the very last occurrence of ">
that exists in the input string.
None-greedy selector
If you make the selector none-greedy, then it will stop at the shortest possible match:
preg_match('/href="http:\/\/(.+?)">/', $a, $b);
Note the extra ?
in the regex pattern, which modifies the "one or more" (+
) operator to be none greedy.
You can also make all selectors none-greedy by default by using the u
modifier:
preg_match('/href="http:\/\/(.+)">/u', $a, $b);