Page 1 of 1
NEWB! Need help with preg_match
Posted: Fri Feb 24, 2006 3:16 pm
by tmaiden
I am trying to catch values in this line.
Code: Select all
<a href="javascript: void(0);" id="ctl00__MessageFriendLink" onclick="javascript: up_myfunction( '1234567', '222222', '', 0 ); return false;">
I only want to catch the information "1234567" here is what I have and what I'm using.
Code: Select all
preg_match("/up_myfunction( '([0-9])'/", $contents, $matches);
Any help would be greatly appriciated!
Posted: Fri Feb 24, 2006 3:30 pm
by feyd
add a plus immediately following the closing bracket (]) inside the parentheses.
Posted: Fri Feb 24, 2006 3:39 pm
by tmaiden
I now have this
Code: Select all
preg_match("/up_myfunction( '[0-9]+'/", $contents, $matches);
And I get this error.
Code: Select all
Warning: Compilation failed: missing ) at offset 21 in /home/www/www.mydomain.com/mypage.php on line 9
Posted: Fri Feb 24, 2006 4:06 pm
by feyd
put a backslash before the first opening paren inside the string.
Code: Select all
preg_match("/up_myfunction( '[0-9]+'/", $contents, $matches);
// ^
You'll need to put the parentheses around the [0-9]+ if you want the number..
Posted: Fri Feb 24, 2006 5:16 pm
by tmaiden
Thats what I have and it seems like Im still getting an error.
Code: Select all
Warning: Compilation failed: missing ) at offset 21 in /home/www/www.mydomain.com/mypage.php on line 9
Posted: Fri Feb 24, 2006 5:29 pm
by SKDevelopment
Code: Select all
<?php
$contents = <<<CONT
<a href="javascript: void(0);" id="ctl00__MessageFriendLink" onclick="javascript: up_myfunction( '1234567', '222222', '', 0 ); return false;">
CONT;
preg_match("/up_myfunction\(\s*'([0-9]+)',/", $contents, $matches);
echo $matches[1];
?>
--
Best Regards,
Sergey Korolev
www.skdevelopment.com
Posted: Fri Feb 24, 2006 6:50 pm
by tmaiden
Thanks for all the help. This is really starting to make sense to me.
Now my script is being ran from another site. How can I have $contents equal to the text that is loaded from the page that is calling it? I tried this but it doesn't seem to be working.
Code: Select all
$ch = curl_init();
$timeout = 3;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
Posted: Fri Feb 24, 2006 7:32 pm
by feyd
What doesn't seem to be working? Not to sound condescending, but did you forget to echo $contents?