Jump to content

Archived

This topic is now archived and is closed to further replies.

kektex

SOLVED - What might be wrong with this code?

Recommended Posts

[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 9

I´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!

Share this post


Link to post
Share on other sites
What do you see with...

[code=php:0]
print_r($result);
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Now I get this:
Array ( [0] => Array ( ) [1] => Array ( ) )

I´m guessing the regular expression is not right and the array is empty?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.