Jump to content

Finding $x.xx/$x.xx


dde

Recommended Posts

Okay I'm done with searching for answers, working on this preg_match for about 3 hours now.

 

I'm looking for "($0.01/$0.02 USD)" in a string. The needle might be slighty different. Possible strings that I might look for is:

(€xx.xx/€xx.xx EUR) where the EUR can be changed into USD, including its signs.

Link to comment
https://forums.phpfreaks.com/topic/293961-finding-xxxxxx/
Share on other sites

Your current regex will also accept "$00.00/€00.00". Sounds like it has to be $/$ USD or €/€ EUR? Go for the most obvious answer:

#\((\$\d+\.\d+/\$\d+\.\d+ USD|€\d+\.\d+/€\d+\.\d+ EUR)\)#
But you're dealing with non-ASCII characters so there's a little bit more work to do.

1. Use Unicode in the regex, including an escape sequence for the Euro sign (or else it will depend on your file encoding). '€' is U+20AC. The /u flag is required and enables UTF-8 mode.

'#\((\$\d+\.\d+/\$\d+\.\d+ USD|\x{20AC}\d+\.\d+/\x{20AC}\d+\.\d+ EUR)\)#u'
2. Make sure your $haystack is in UTF-8.

 

$euro = "\xe2\x82\xac"; // utf-8 encoding of \x{20AC}
$haystack = array(
	'($00.00/$00.00 USD)', // match
	'($00.00/' . $euro . '00.00 USD)', // no match
	'(' . $euro . '00.00/$00.00 EUR)', // no match
	'(' . $euro . '00.00/' . $euro . '00.00 EUR)' // match
);

$regex = '#\((\$\d+\.\d+/\$\d+\.\d+ USD|\x{20AC}\d+\.\d+/\x{20AC}\d+\.\d+ EUR)\)#u';
foreach ($haystack as $h) {
	var_dump(preg_match($regex, $h));
}
Link to comment
https://forums.phpfreaks.com/topic/293961-finding-xxxxxx/#findComment-1503092
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.