Remove non-numeric characters (excluding periods and commas) from a string (i.e. remove all characters except numbers, commas, and periods)
You could use preg_replace to swap out all non-numeric characters and the comma and period/full stop as follows:
$testString = '12.322,11T';
echo preg_replace('/[^0-9,.]+/', '', $testString);
The pattern can also be expressed as /[^\d,.]+/
PHP - remove all non-numeric characters from 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,.]/"
How to remove all non numeric characters (excluding minus, dot and comma) in a string in Javascript?
To remove all chars but digits, commas, dots and a hyphen at the start of the string, you may use
text = text.replace(/^(-)|[^0-9.,]+/g, '$1');
See the regex demo
Details
^(-)
- start of string and a-
captured into Group 1|
- or[^0-9.,]+
- any 1+ chars other than digits,.
and,
.
The replacement is $1
, i.e. if there was a leading -
it will remain in the result.
A bit more comprehensive regex that only keeps the last non-final comma/dot is
text = text.replace(/^(-)|[.,](?=[^.,]*[.,](?!$))|[,.]+$|[^0-9.,]+/g, '$1');
See this regex demo
Here, some more alternatives are added:
[.,](?=[^.,]*[.,](?!$))
- matches.
or,
that are followed with another.
or,
somewhere after 0+ chars other than.
and,
[,.]+$
- matches any 1+ trailing commas/dots.
Get Number only without any text
Get all the numbers before 'Indonesische roepia' with a regex:
/([0-9.,]+)\ Indonesische roepia/
<?php
$url ='https://www.google.com/search?q=1+usd+to+idr';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$curl_response = curl_exec($curl);
curl_close($curl);
if(!preg_match_all("/<div[^>]*class=\"\s*BNeawe\s+iBp4i\s+AP7Wnd\s*\"[^>]*>(.*?)<\/div>/s", $curl_response, $result, PREG_SET_ORDER)){
echo 'Invalid output';
exit;
}
$re = '/([0-9.,]+)\ Indonesische roepia/';
preg_match($re, $result[0][1], $number);
echo '<pre>';
print_r((float) $number[1]);
echo '</pre>';
<pre>14.391</pre>
Strip all non-numeric characters from string in JavaScript
Use the string's .replace
method with a regex of \D
, which is a shorthand character class that matches all non-digits:
myString = myString.replace(/\D/g,'');
Java String remove all non numeric characters but keep the decimal separator
Try this code:
String str = "a12.334tyz.78x";
str = str.replaceAll("[^\\d.]", "");
Now str
will contain "12.334.78"
.
Removing (nearly) all non-numeric characters from a string
If I understand you correctly you can use this:
$re = "/[0-9XxTtEe+:]*/";
$str = "394160etg9834ztg"; // <-- User Input
preg_match_all($re, $str, $matches);
In $matches
should be all characters that are allowed. Just combine the results that are matched in $matches
Preg_replace all but digits and . and ,
Use [^0-9\.,]
as your matching expression.
Removing (nearly) all non-numeric characters from a string
If I understand you correctly you can use this:
$re = "/[0-9XxTtEe+:]*/";
$str = "394160etg9834ztg"; // <-- User Input
preg_match_all($re, $str, $matches);
In $matches
should be all characters that are allowed. Just combine the results that are matched in $matches
Related Topics
PHP - Remove <Img> Tag from String
How to Use Regexiterator in PHP
How to Change/Custom Password Field Name for Laravel 4 and Laravel 5 User Authentication
Is PHP's Count() Function O(1) or O(N) for Arrays
PHP Form - on Submit Stay on Same Page
Mongodb: Fatal Error: Class 'Mongoclient' Not Found
How to Add an Order by Clause Using Codeigniter's Active Record Methods
Dom Parser That Allows HTML5-Style </ in <Script> Tag
How to Insert Multiple Checkbox Values into a Table
Php. How to Use Array_Column with an Array of Objects
Are There Limits for Session Variables
How to Use Prepared Statements in Codeigniter
How to Merge Transparent Png with Image Using PHP
The Difference Between Unset and = Null
Include, Include_Once, Require or Require_Once
PHP Function to Delete All Between Certain Character(S) in String
What Delimiter to Use for Preg_Replace in PHP (Replace Working Outside of PHP But Not Inside)