skyline
1
Hi,
I want to find my link within the html string. currently I’m using this:
$url_to_find = "domain.co.uk";
if(preg_match('#<a.+?href=[\\'"]http://(www\\.)?'.$url_to_find.'[\\'"].+?>#i',$html,$matches))....
which works well for the exact url:
but doesn’t for a subpage link:
http://www.domain.co.uk/subpage.html
How can I modify this code to allow for any links within the domain?
dsims
2
$html = <<<FIL
<a href="http://www.domain.co.uk"></a>
<a href='http://www.domain.co.uk/subpage.html'></a>
FIL;
$url_to_find = "domain.co.uk";
$url_to_find = str_replace('.', '\\.', $url_to_find);
preg_match('#a[\\s]+href[\\s]*=[\\s]*(?:\\'|")(http://?(www\\.)' . $url_to_find . '.*)(?:\\'|")#i',$html,$matches);
var_dump(preg_last_error());
var_dump($matches[1]);
if you want all matches use preg_match_all
if you really want to explore regular expression I would advise downloading Kodos it’s a god send
skyline
3
thanks i’ll give it a try
lorenw
4
$url_to_find.'*.?\>
Just a stab in the dark. addedbytes.com has an awesome regex cheat sheet.