stripping out all characters from a string, leaving numbers - php

Hay, i have a string like this:
v8gn5.8gnr4nggb58gng.g95h58g.n48fn49t.t8t8t57
I want to strip out all the characters leaving just numbers (and .s)
Any ideas how to do this? Is there a function prebuilt?
thanks

$str = preg_replace('/[^0-9.]+/', '', $str);
replace substrings that do not consist of digits or . with nothing.
Here's how it works:
preg_replace is a PHP function that searches a string for a pattern and replaces it with a given replacement string.
The first parameter in preg_replace is the regular expression pattern to search for. In this case, the pattern is '/[^0-9.]+/', which matches any character that is not a digit or a dot. The ^ character inside square brackets means "not", so [^0-9.] means any character that is not a digit or a dot. The + sign means one or more occurrences of the previous character or character group, in this case [^0-9.].
The second parameter in preg_replace is the replacement string. In this case, the replacement string is an empty string ''. So any character that matches the pattern in the first parameter will be replaced with an empty string.
The third parameter in preg_replac is the input string to search and modify. In this case, the input string is represented by the variable $str.
So, this line of code will remove any character from the input string $str that is not a digit or a dot, and return the modified string with only digits and dots.

preg_replace('/[^0-9.]/', '', $string);

$input = 'some str1ng 234';
$newString = preg_replace("/[^0-9.]/", '', $input);

To satisfy my curiosity I asked about the speed of the proposed answers and as shown in preg_replace speed optimisation/ it is (much) faster to use str_replace() than preg_replace().
So you might want to use str_replace() instead.

Here is the shortest one:
$str = preg_replace('/\D/', '', $str);
\D = all non-digits.

Related

preg_replace all non-digit characters except the number between second and third slash

Assuming the input string /en/614/5d or /en/617/19, using PHP's regex functions I would like to remove all non-digit (/\D/) characters, except the 614 or 617 between the second and third slashes.
I know I can use split or explode to split the string but I need a regular expression pattern to use with preg_replace to extract only the numbers between second and third slashes.
Thanks.
preg_replace('/^\/[^\/]+\/|\/.+$/','',$string);
this should be better and simple I think
preg_replace('/\D/', '', $input_lines); //replace any non digit

Last character of a string should be a letter or a number

I have these string:
$string = 'Tester,';
Finally I would like to get:
'Tester'
I tried like that:
$string = rtrim($string, array(',', '.'));
but second argument of rtrim() couldn't be an array.
How I can remove from end of a string everything what isn't letter or number?
Thanks.
This should work for you:
(Here I just check if the last character of the string isn't alphanumeric wtih ctype_alnum(). If true I set the last character empty otherwise I leave it)
<?php
$string = "Tester,";
$string[strlen($string)-1] = (!ctype_alnum($string[strlen($string)-1])?"":$string[strlen($string)-1]);
echo $string;
?>
output:
Tester
You could use preg_match() to remove any non alpha-numeric characters from end of string like this:
preg_match('/(.+[a-zA-Z0-9])/',$string,$matches);
$string = $matches[0];
Another solution are regular expressions.
This solves your task: preg_replace('/(.*)[^a-zA-Z0-9]$/', '$1', 'Tester,');
Regex in action:
Online regex tester and debugger
Codeshare.io - PHP Sample
Description
The regular expression used for this task looks for a string that starts with any character ^(.*) and does not end with a (alpha-)numeric character [^a-zA-Z0-9]$. The preg_replace replaces the match with the value of the so called backreference. A backreference is a reference to the value of a group. A group is everything between curved brackets.
Links
Regex reference

Regular Expressions (php)- match blocks of non alphanumerical charactors

I'm in need to modify a given string to contain only alpha numerical characters, dots (.) and commas.
If the string contains any character other than a-z, A-Z , 0-9 or a dot(.), they should be replaced with a comma sign, I'm using this:
$string = "dycloro 987 stackOVERflow !|,!!friday";
$newstring = preg_replace('/[^a-zA-Z0-9\.]/', ',', $string);
This returns,
dycloro,987,stackOVERflow,,,,,,friday
But I'm in need to get the following instead.
dycloro,987,stackOVERflow,friday
(Note the " !|,!!" part in $string is replaced with a single comma sign).
Ideally, I want to replace a block of disallowed characters with a single comma sign.
I figured out that
$newstring = preg_replace('/,{2,}/', ',', $newstring); replaces multiple comma signs with a single comma. But is there any way to do this in a faster, or better way ?
How do I do this in a single regular expression match ?
and is there any process time or memory difference in them ? This is regular expressions will be run against few megabytes of user input so I'm curious about it as well.
Thank you!
Just add a plus sign +, meaning "one or more of what I just mentioned", after the character class:
$string = "dycloro 987 stackOVERflow !|,!!friday";
$newstring = preg_replace('/[^a-zA-Z0-9\.]+/', ',', $string);
See http://www.php.net/manual/en/regexp.reference.repetition.php.
Try this one
$newstring = preg_replace('/[^a-zA-Z0-9\.]+/', ',', $string);

Returning only 0-9 and dashes from string

I would like to take a string, and strip any characters apart from 0-9 and - (dashes).
Example:
if I have a string that looks like:
10-abc20-30
How can I make this string return
10-20-30
(Strip all characters besides numbers and dashes)
Is there some kind of regex to use within preg_match or str_replace ?
$result = preg_replace('/[^\d-]+/', '', $subject);
[^\d-] matches any character except digits or dash; the + says "one or more" of those, so adjacent characters will be replaced at once.
Assuming your data is in $string, this will remove all characters except for dashes and digits
$string = preg_replace('/[^-0-9]/', null, $string);

Replace all spaces and special symbols with dash in URL using PHP language

How to replace spaces and dashes when they appear together with only dash in PHP?
e.g below is my URL
http://kjd.case.150/1 BHK+Balcony- 700+ sqft. spacious apartmetn Bandra Wes
In this I want to replace all special characters with dash in PHP. In the URL there is already one dash after "balcony". If I replace the dash with a special character, then it becomes two dashes because there's already one dash in the URL and I want only 1 dash.
I'd say you may be want it other way. Not "spaces" but every non-alphanumeric character.
Because there can be other characters, disallowed in the URl (+ sign, for example, which is used as a space replacement)
So, to make a valid url from a free-form text
$url = preg_replace("![^a-z0-9]+!i", "-", $url);
If there could be max one space surrounding the hyphen you can use the answer by John. If there could be more than one space you can try using preg_replace:
$str = preg_replace('/\s*-\s*/','-',$str);
This would replace even a - not surrounded with any spaces with - !!
To make it a bit more efficient you could do:
$str = preg_replace('/\s+-\s*|\s*-\s+/','-',$str);
Now this would ensure a - has at least one space surrounding it while its being replaced.
This should do it for you
strtolower(str_replace(array(' ', ' '), '-', preg_replace('/[^a-zA-Z0-9 s]/', '', trim($string))));
Apply this regular expression /[^a-zA-Z0-9]/, '-' which will replace all non alphanumeric characters with -. Store it in a variable and again apply this regular expression /\-$/, '' which will escape the last character.
Its old tread but to help some one, Use this Function:
function urlSafeString($str)
{
$str = eregi_replace("[^a-z0-9\040]","",str_replace("-"," ",$str));
$str = eregi_replace("[\040]+","-",trim($str));
return $str;
}
it will return you a url safe string

Categories