Jump to content


Photo

SOLVED - What might be wrong with this code?


  • Please log in to reply
5 replies to this topic

#1 kektex

kektex
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 28 October 2006 - 12:45 AM

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!

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.c...le.com">Example Anchor</a>

I´m using this code to extract the URL:
$data = file_get_contents('file.html');
preg_match_all("/<a style=\"clear:all\;\" href=\"(.+?)\"> /", $data, $result);

$result = $result[3];
echo $result."<br />";


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!

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 28 October 2006 - 12:56 AM

What do you see with...

print_r($result);


#3 kektex

kektex
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 28 October 2006 - 01:04 AM

I get the same error when I substitute echo $result."<br />";  with  print_r($result);:

Notice: Undefined offset: 3 in c:\xxxxx.php on line 9


#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 28 October 2006 - 01:42 AM

Replace this...

$result = $result[3];

with...

print_r($result);

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.

#5 kektex

kektex
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 28 October 2006 - 01:51 AM

Now I get this:
Array ( [0] => Array ( ) [1] => Array ( ) )

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

#6 kektex

kektex
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 28 October 2006 - 02:16 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users