Forum Moderators: coopster
I have the following code:
$text="<ListPrice>15</ListPrice>";
preg_match('/<ListPrice>(\d+)<\/ListPrice>/s',$text,$matches);
echo $matches[0];
My goal is to extract the number inside the ListPrice tags in a decimal format,not as a string.The code above outputs 15,however, when I try to convert it to a decimal using the intval command,for some reason I get the value zero.How do I solve this?
Thanks.
<?php
$text="<ListPrice>15</ListPrice>";
preg_match('/<ListPrice>(\d+)<\/ListPrice>/s',$text,$matches);if(is_string($matches[1])) {
$matches[1]=intval($matches[1]);
echo 'Converted to intval();<br>';
echo $matches[1]."<br>";
echo htmlentities($matches[0]);
}else {
echo 'Already a Numeric Value<br>';
echo $matches[1]."<br>";
echo htmlentities($matches[0]);
}
?>
EDITED
echo htmlspecialchars($matches[0]); As explained by the preg_match documentation [us.php.net] (emphasis mine):
$matches[0] will contain the text that matched the full pattern