You can use preg_replace in this case;
$res = preg_replace("/[^0-9]/", "", "Every 6 Months" );$res return 6 in this case.
If want also to include decimal separator or thousand separator check this example:
$res = preg_replace("/[^0-9.]/", "", "$ 123.099");$res returns "123.099" in this case
Include period as decimal separator or thousand separator: "/[^0-9.]/"
Include coma as decimal separator or thousand separator: "/[^0-9,]/"
Include period and coma as decimal separator and thousand separator: "/[^0-9,.]/"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
How do I remove all non numeric characters from a string in PHP?
Method 1: The regular expression '/[\W]/' matches all the non-alphanumeric characters and replace them with ' ' (empty string). $str = preg_replace( '/[\W]/', '', $str);
How remove all special characters from a string in PHP?
Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).
How do I remove a word from a string in PHP?
Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.
You can use preg_replace in this case;
$res = preg_replace("/[^0-9]/", "", "Every 6 Months" );$res return 6 in this case.
If want also to include decimal separator or thousand separator check this example:
$res = preg_replace("/[^0-9.]/", "", "$ 123.099");$res returns "123.099" in this case
Include period as decimal separator or thousand separator: "/[^0-9.]/"
Include coma as decimal separator or thousand separator: "/[^0-9,]/"
Include period and coma as decimal separator and thousand separator: "/[^0-9,.]/"
Use \D to match non-digit characters.
preg_replace('~\D~', '', $str);php remove non numeric – Remove or strip all characters except for alphanumeric and underscore and dash
php remove non numeric
- php remove non numeric
- How to remove non-numeric characters except comma and dot in php?
- PHP – Remove all non-numeric characters
- Remove nonnumeric characters except comma and dot
- Related posts
PHP – remove all non-numeric characters from a string
<?php $str = "tu!tor^ials$%!poi&nt"; echo preg_replace('/[^a-zA-Z0-9]/','', $str); ?>How to remove non-numeric characters except comma and dot in php?
Example
PHP – Remove all non-numeric characters
Example
Remove nonnumeric characters except comma and dot
Example
SQL Remove Numeric Characters In String
php remove characters not numbers or letters
I hope you get an idea about php remove non numeric.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments
about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.
PHP regular expression: Exercise-4 with Solution
Write a PHP script to remove nonnumeric characters except comma and dot.
Sample string : '$123,34.00A'
Pictorial Presentation:
Sample Solution:
PHP Code:
<?php $str1 = "$12,334.00A"; echo preg_replace("/[^0-9,.]/", "", $str1)."\n"; ?>Sample Output:
12,334.00Flowchart :
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script that removes the whitespaces from a string.
Next: Write a PHP script to
remove new lines (characters) from a string.
PHP: Tips of the Day
PHP: PHP Constants Containing Arrays?
NOTE: while this is the accepted answer, it's worth noting that in PHP 5.6+ you can have const arrays
You can also serialize your array and then put it into the constant:
# define constant, serialize array define ("FRUITS", serialize (array ("apple", "cherry", "banana"))); # use it $my_fruits = unserialize (FRUITS);Ref : //bit.ly/2EcIer8