Page 1 of 1

preg_match help

Posted: Tue Jul 05, 2011 1:49 pm
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

Re: preg_match help

Posted: Wed Jul 06, 2011 10:09 am
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 >

Re: preg_match help

Posted: Wed Jul 06, 2011 12:26 pm
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

Re: preg_match help

Posted: Wed Jul 06, 2011 1:01 pm
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);
 
?>

Re: preg_match help

Posted: Wed Jul 06, 2011 1:11 pm
by rascle
Thanks, however nothing is happening still :(