zzz;10907215 wrote:Great, code worked. However, since the last post I decided to ad some additional functionality and now need to introduce a whole new level of complexity.
In this new code I need to strip just the basic URL (http://www.sdfasdasd.com)
I modified the earlier code to this:
$pattern = '/href[^\>]*\//i';
if (preg_match_all($pattern, $str, $matches)) {
print_r($matches);
}
However what I get is something like this:
Array
(
[0] => Array
(
[0] => href="http://www.tdkqlhce.com/
)
)
Array
(
[0] => Array
(
[0] => href="http://www.jddoqocy.com/
)
)
Array
(
[0] => Array
(
[0] => href="http://www.jddoqocy.com/
)
)
Now, arrays and regex always throw me deep into the woods... this has both. What i need now is to strip href=" and compile a list of only unique URLs. I know I'm close to the goal but do need help of someone who's not intimidated by this stuff, like me....
The reason you got all that ZZZ was beacuse you used preg_match_all
preg_match only finds the first match, so only does a one dimensional array, eg:
array()
{
[0] = "href="http://www.jddoqocy.com/"
}
preg_match_all finds all matches in the whole text, and if there is more than one it builds a multidimensional array as your code above shows.
it's easy enough however to access the multiples.
$array[0][0] will get the first
$array[0][1] will get the second
$array[0][2] the third....
and so on...
if there is 1 match or less in each of them then you'll just get several single arrays with in a single array (Which is what your doing), so:
$array[0][0] will always get the url you found.
If your ONLY expecting one url to be found in each string, then your much better just using preg_match($pattern,$subject,$results)
Cheers
Shawty