Jump to content


Photo

Strange string to double conversion when using preg_match


  • Please log in to reply
2 replies to this topic

#1 EirikE

EirikE
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 October 2006 - 11:30 PM

I have a very strange problem with type hinting of a String from a preg_match-function. It seems like whatever I do, the string does not get converted into a double properly, it just becomes 0. <b>Edit:</b> I have also tried using string functions to do the same(strpos,str_replace etc.) I also need to mention that the $xml-variable is from a XML file read with DOM. I don't know if that matters.

PHP-code:
<code>
$xml = "<date>1136070000</date>";
preg_match("/<date>([0-9]*)<\/date>/",$xml,$matches);
echo "String: " . $matches[0];
echo "<br />";
echo "Double: " . ((double) $matches[0]);         
</code>

Outputs:
<code>
String: 1136070000
Double: 0                                           
</code>

Any suggestions?

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 16 October 2006 - 02:27 PM

$matches[0] is the entire match--you want $matches[1], the first (...) matched.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 EirikE

EirikE
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 16 October 2006 - 03:43 PM

That's solves it. Thanks a lot :). I actually ended up using php-functions in xslt.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users