Jump to content

SOLVED - What might be wrong with this code?


kektex

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.