Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to pull out using preg match

10:55 pm on Jan 19, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 10, 2007
votes: 0

I am terrible at preg_match in php. Have tried alot to get the following to work.

$pattern = 'Trader Rating: (<a href="itrader.php?u=1111">122</a>)';
//$pattern = preg_quote($pattern);
$pattern = '/' . $pattern .'/';
echo $pattern;
preg_match($pattern, $page, $matches);

Right now it prints out an empty array, $page is just an html page. I need to match the pattern. With 1111 being any number and 122 matching any number. Then I need to pull the 122 number out of it so I know what that number is.

[edited by: eelixduppy at 1:52 am (utc) on Jan. 20, 2008]
[edit reason] disabled smileys [/edit]

7:21 am on Jan 20, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 16, 2004
posts: 96
votes: 0

RE: $page is just an html page

You should not pass the html page name, but its content. So, use file_get_content() function to read the html page code into a string.

Lets suppose $page looks like:

$page = 'Trader Rating: <a href="itrader.php?u=1111">122</a>';

Than, your pattern will be:
$pattern = "/Trader Rating: \<a href=\"itrader.php\?u=\d{1,}\">(\d{1,})<\/a>/";

and the $matches will contain this data:

[0] => Trader Rating: <a href="itrader.php?u=1111">122</a>
[1] => 122

The result you are looking for is stored at $matches['1'] element.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members