homepage Welcome to WebmasterWorld Guest from 54.145.209.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to pull out using preg match
8kobe

5+ Year Member



 
Msg#: 3552738 posted 10:55 pm on Jan 19, 2008 (gmt 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);
print_R($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]

 

venelin13

10+ Year Member



 
Msg#: 3552738 posted 7:21 am on Jan 20, 2008 (gmt 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:

Array
(
[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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved