NEWB! Need help with preg_match

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

NEWB! Need help with preg_match

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

add a plus immediately following the closing bracket (]) inside the parentheses.
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post 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
SKDevelopment
Forum Newbie
Posts: 13
Joined: Thu Jan 26, 2006 10:42 am

Post 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
User avatar
tmaiden
Forum Commoner
Posts: 64
Joined: Fri Feb 24, 2006 3:15 pm
Location: Philadelphia
Contact:

Post 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);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What doesn't seem to be working? Not to sound condescending, but did you forget to echo $contents?
Post Reply