preg_match help

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
rascle
Forum Newbie
Posts: 4
Joined: Tue Jul 05, 2011 1:42 pm

preg_match help

Post by rascle »

Hi,
Basically I am trying to use the code below:

Code: Select all

<?php
$eurovalue = file_get_contents("dump.txt");
$eurovalue = htmlspecialchars($eurovalue);
$pattern = '/<td><a.*>USD\/EUR<\/a><\/td>.*<td\s*\w*>\s*<img\s*[\w\d]*\s*\/>(\d+)<\/td>/si';
preg_match($pattern,$eurovalue,$euroarray);
$euroresult = $euroarray[0];
echo $euroresult;

?>
To find this code:

Code: Select all

<td><a href="/business/currencies/quote?srcAmt=1&srcCurr=USD&destAmt=&destCurr=EUR">USD/EUR</a></td>
		<td class="data changeUp">
			<img width="9" height="10" src="/resources_v2/images/changeUp.gif" />0.69070</td>
In a script that contains similar <td>'s, also the 0.69070 is subject to change. The preg_match that I am using isnt working and I cant work out why. I imagine it is something to do with the regex. The code that it is searching for is located in dump.text and this is all working fine, it just seems to be the preg_match.
Any help would be appreciated,
Thanks
Rhys
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: preg_match help

Post by social_experiment »

Code: Select all

<?php
$eurovalue = htmlspecialchars($eurovalue);
?>
htmlspecialchars() convert html characters ( < , > , " , ' , & ) to html safe values so when you check the data against the pattern there is no < or > because they were converted to < and >
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rascle
Forum Newbie
Posts: 4
Joined: Tue Jul 05, 2011 1:42 pm

Re: preg_match help

Post by rascle »

Thanks for that :)
Do you know how I could do something similar without it affecting my Regex code? E.g rather than display the HTML code, write it in normal?
Or would you know how to change the regex?
Thanks
Rhys
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: preg_match help

Post by flying_circus »

rascle wrote:Thanks for that :)
Do you know how I could do something similar without it affecting my Regex code? E.g rather than display the HTML code, write it in normal?
Or would you know how to change the regex?
Thanks
Rhys
Run the regex before converting special characters

Code: Select all

<?php
 $eurovalue = file_get_contents("dump.txt");
 //$eurovalue = htmlspecialchars($eurovalue);
 $pattern = '/<td><a.*>USD\/EUR<\/a><\/td>.*<td\s*\w*>\s*<img\s*[\w\d]*\s*\/>(\d+)<\/td>/si';
 preg_match($pattern,$eurovalue,$euroarray);
 $euroresult = $euroarray[0];
 echo htmlspecialchars($euroresult);
 
?>
rascle
Forum Newbie
Posts: 4
Joined: Tue Jul 05, 2011 1:42 pm

Re: preg_match help

Post by rascle »

Thanks, however nothing is happening still :(
Post Reply