0

I'm trying to remove all non-numeric characters from a string. This is the string:

$price = '₪1,180.00';

(the first character is the new israeli shekel currency symbol)

I tried to use:

$price_numeric_value = preg_replace( '/\D/', '', $price );
echo '<pre>';var_dump( $price_numeric_value );echo '</pre>';

$price_numeric_value = preg_replace( '~\D~', '', $price );
echo '<pre>';var_dump( $price_numeric_value );echo '</pre>';

$price_numeric_value = preg_replace( '/[^0-9.]/', '', $price );
echo '<pre>';var_dump( $price_numeric_value );echo '</pre>';

As suggested in these posts:
https://stackoverflow.com/a/34399544/4711865
https://stackoverflow.com/a/33993704/4711865

The output i'm getting is this:

string(10) "8362118000"

Any idea why?

EDIT: I'm running this code on a Wordpress website, the php file is encoded in utf-8 and adding header('Content-Type: text/html; charset=utf-8'); doesn't help.

odedta
  • 2,430
  • 4
  • 24
  • 51
  • 4
    `preg_replace( '/\D/', '', $price );` returns `118000`, see https://ideone.com/M7DF2Q – Wiktor Stribiżew Nov 13 '18 at 14:29
  • @WiktorStribiżew Hey, I see the link you sent, is it possible that the preg_replace function that is running on my site is affected by something? i'm running this on Wordpress inside a template file. – odedta Nov 13 '18 at 14:32
  • 2
    Check the UTF8 encoding of the files. Try `preg_replace( '/\D+/u', '', $price );`, too. – Wiktor Stribiżew Nov 13 '18 at 14:34
  • Even if the file is Ascii encoded the result of `preg_replace( '/\D/', '', $price );` is 118000 – RiggsFolly Nov 13 '18 at 14:37
  • @WiktorStribiżew Same result, the file is encoded in utf-8 and I even tried with `header('Content-Type: text/html; charset=utf-8');`, same result. :/ – odedta Nov 13 '18 at 14:39
  • 1
    Are you sure there is no other interfering code somewhere on the way from `$price = '₪1,180.00';` to `preg_replace( '/\D/', '', $price )`? – Wiktor Stribiżew Nov 13 '18 at 14:52
  • 1
    @WiktorStribiżew Yes! I literally place them one below the other... that's the reason I posted this, somehow the value is being mixed up! – odedta Nov 13 '18 at 14:59

1 Answers1

3

8362 is the numeric part of the html entity for the New Sheqel Sign &#8362;, when you remove all non numeric you got 8362 just before the value.

You have to decode the string before preg_replace.

$price_numeric_value = preg_replace( '/\D/', '', html_entity_decode($price) );
echo $price_numeric_value;
Toto
  • 89,455
  • 62
  • 89
  • 125