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! Link to comment https://forums.phpfreaks.com/topic/25370-solved-what-might-be-wrong-with-this-code/ 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] Link to comment https://forums.phpfreaks.com/topic/25370-solved-what-might-be-wrong-with-this-code/#findComment-115663 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 Link to comment https://forums.phpfreaks.com/topic/25370-solved-what-might-be-wrong-with-this-code/#findComment-115665 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. Link to comment https://forums.phpfreaks.com/topic/25370-solved-what-might-be-wrong-with-this-code/#findComment-115670 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? Link to comment https://forums.phpfreaks.com/topic/25370-solved-what-might-be-wrong-with-this-code/#findComment-115671 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. Link to comment https://forums.phpfreaks.com/topic/25370-solved-what-might-be-wrong-with-this-code/#findComment-115677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.