dde Posted January 15, 2015 Share Posted January 15, 2015 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. Quote Link to comment Share on other sites More sharing options...
dde Posted January 15, 2015 Author Share Posted January 15, 2015 preg_match("/[\$\€]\d+[.]\d+\/[\$\€]\d+[.]\d+/", $haystack, $matches); Matches €00.00/€00.00 as well as $00.00/$00.00. Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted January 15, 2015 Solution Share Posted January 15, 2015 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)); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.