Jump to content

Finding $x.xx/$x.xx


dde
Go to solution Solved by requinix,

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
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.