kektex Posted October 28, 2006 Share Posted October 28, 2006 [b]I found the error in the code...I had some blank spaces in a place there shouldn´t have been any.The regex was correct.Thanks for the help thorpe![/b]Hello,Im trying to extract all URLs from an html file.The links I´m interested in have this formatting:<a style="clear:all;" href="http://www.example.com">Example Anchor</a>I´m using this code to extract the URL:[code]$data = file_get_contents('file.html');preg_match_all("/<a style=\"clear:all\;\" href=\"(.+?)\"> /", $data, $result);$result = $result[3];echo $result."<br />";[/code]All I get is the following error:Notice: Undefined offset: 3 in c:\xxxxx.php on line 9I´m a newbie to PHP and I´ve been reading about regex and arrays but I can´t seem to find what´s wrong here.I´m thinking it´s the array since the error says something about the offset.I´m just basing this on some code I found that does a similar job to the one I need to do.Thanks for any help! Quote Link to comment Share on other sites More sharing options...
trq Posted October 28, 2006 Share Posted October 28, 2006 What do you see with...[code=php:0]print_r($result);[/code] Quote Link to comment Share on other sites More sharing options...
kektex Posted October 28, 2006 Author Share Posted October 28, 2006 I get the same error when I substitute [color=blue]echo $result."<br />";[/color] with [color=blue]print_r($result);[/color]:Notice: Undefined offset: 3 in c:\xxxxx.php on line 9 Quote Link to comment Share on other sites More sharing options...
trq Posted October 28, 2006 Share Posted October 28, 2006 Replace this...[code=php:0]$result = $result[3];[/code]with...[code=php:0]print_r($result);[/code]The error is indicating that there is no 3rd arraay element. Im sorry, I'm no good with regular exressions or I might be of more assistance, at least this way though we'll be able to see what $result looks like. Quote Link to comment Share on other sites More sharing options...
kektex Posted October 28, 2006 Author Share Posted October 28, 2006 Now I get this:Array ( [0] => Array ( ) [1] => Array ( ) ) I´m guessing the regular expression is not right and the array is empty? Quote Link to comment Share on other sites More sharing options...
kektex Posted October 28, 2006 Author Share Posted October 28, 2006 I just checked the regex with this tool : http://regexlib.com/RETester.aspx and it works great.Maybe theres something wrong with my php configuration?I have a similar script running without any problems so I dont think that´s the problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.