Remove all non-numeric characters from a string in PHP












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.










share|improve this question




















  • 4





    preg_replace( '/D/', '', $price ); returns 118000, see 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








  • 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
















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.










share|improve this question




















  • 4





    preg_replace( '/D/', '', $price ); returns 118000, see 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








  • 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














0












0








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.










share|improve this question
















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.







php preg-replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:45







odedta

















asked Nov 13 '18 at 14:28









odedtaodedta

1,6281934




1,6281934








  • 4





    preg_replace( '/D/', '', $price ); returns 118000, see 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








  • 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














  • 4





    preg_replace( '/D/', '', $price ); returns 118000, see 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








  • 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








4




4





preg_replace( '/D/', '', $price ); returns 118000, see ideone.com/M7DF2Q

– Wiktor Stribiżew
Nov 13 '18 at 14:29





preg_replace( '/D/', '', $price ); returns 118000, see 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





@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




2





Check the UTF8 encoding of the files. Try preg_replace( '/D+/u', '', $price );, too.

– Wiktor Stribiżew
Nov 13 '18 at 14:34







Check the UTF8 encoding of the files. Try preg_replace( '/D+/u', '', $price );, too.

– Wiktor Stribiżew
Nov 13 '18 at 14:34






1




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





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




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





@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 Answer
1






active

oldest

votes


















1














8362 is the numeric part of the html entity for the New Sheqel Sign , 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;





share|improve this answer


























  • Amazing, how do you know this ;-) thank you very much @Toto Please update your code: html-entity-decode change to html_entity_decode

    – odedta
    Nov 14 '18 at 5:37













  • @odedta: Thanks, typo corrected. It was just a guess then I checked the code of New Sheqel Sign, Bingo!

    – Toto
    Nov 14 '18 at 8:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283229%2fremove-all-non-numeric-characters-from-a-string-in-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














8362 is the numeric part of the html entity for the New Sheqel Sign , 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;





share|improve this answer


























  • Amazing, how do you know this ;-) thank you very much @Toto Please update your code: html-entity-decode change to html_entity_decode

    – odedta
    Nov 14 '18 at 5:37













  • @odedta: Thanks, typo corrected. It was just a guess then I checked the code of New Sheqel Sign, Bingo!

    – Toto
    Nov 14 '18 at 8:36
















1














8362 is the numeric part of the html entity for the New Sheqel Sign , 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;





share|improve this answer


























  • Amazing, how do you know this ;-) thank you very much @Toto Please update your code: html-entity-decode change to html_entity_decode

    – odedta
    Nov 14 '18 at 5:37













  • @odedta: Thanks, typo corrected. It was just a guess then I checked the code of New Sheqel Sign, Bingo!

    – Toto
    Nov 14 '18 at 8:36














1












1








1







8362 is the numeric part of the html entity for the New Sheqel Sign , 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;





share|improve this answer















8362 is the numeric part of the html entity for the New Sheqel Sign , 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;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 8:34

























answered Nov 13 '18 at 17:53









TotoToto

65.1k175698




65.1k175698













  • Amazing, how do you know this ;-) thank you very much @Toto Please update your code: html-entity-decode change to html_entity_decode

    – odedta
    Nov 14 '18 at 5:37













  • @odedta: Thanks, typo corrected. It was just a guess then I checked the code of New Sheqel Sign, Bingo!

    – Toto
    Nov 14 '18 at 8:36



















  • Amazing, how do you know this ;-) thank you very much @Toto Please update your code: html-entity-decode change to html_entity_decode

    – odedta
    Nov 14 '18 at 5:37













  • @odedta: Thanks, typo corrected. It was just a guess then I checked the code of New Sheqel Sign, Bingo!

    – Toto
    Nov 14 '18 at 8:36

















Amazing, how do you know this ;-) thank you very much @Toto Please update your code: html-entity-decode change to html_entity_decode

– odedta
Nov 14 '18 at 5:37







Amazing, how do you know this ;-) thank you very much @Toto Please update your code: html-entity-decode change to html_entity_decode

– odedta
Nov 14 '18 at 5:37















@odedta: Thanks, typo corrected. It was just a guess then I checked the code of New Sheqel Sign, Bingo!

– Toto
Nov 14 '18 at 8:36





@odedta: Thanks, typo corrected. It was just a guess then I checked the code of New Sheqel Sign, Bingo!

– Toto
Nov 14 '18 at 8:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283229%2fremove-all-non-numeric-characters-from-a-string-in-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings