Strip non Alphanumeric chars from filename - php [duplicate] - php

I'd like a regexp or other string which can replace everything except alphanumeric chars (a-z and 0-9) from a string. All things such as ,##$(#*810 should be stripped. Any ideas?
Edit: I now need this to strip everything but allow dots, so everything but a-z, 1-9, .. Ideas?

$string = preg_replace("/[^a-z0-9.]+/i", "", $string);
Matches one or more characters not a-z 0-9 [case-insensitive], or "." and replaces with ""

I like using [^[:alnum:]] for this, less room for error.
preg_replace('/[^[:alnum:]]/', '', "(ABC)-[123]"); // returns 'ABC123'

Try:
$string = preg_replace ('/[^a-z0-9]/i', '', $string);
/i stands for case insensitivity (if you need it, of course).

/[^a-z0-9.]/
should do the trick

This also works to replace anything not a digit, a word character, or a period with an underscore. Useful for filenames.
$clean = preg_replace('/[^\d\w.]+/', '_', $string);

Related

Replacing everything other than alphanumeric, hyphen, period and underscore in a string

Here is what I currently use to cleanup filenames with special characters. Basically, it should replace everything other than:
Alphanumeric a-zA-Z0-9
Underscores _
Hyphens -
Period .
echo preg_replace("/[^a-zA-Z0-9\_\-\.\s]/", "", "A\'s`kljdsg\\\`sk//dgj###//\/\/\#``flkl_dfd-_-sfsfs-fk kgj.docx");
Although it seems to work, I am not sure if the _-. is the correct way of using it in a regex.
- inside character class denotes range, e.g. 0-9. So, you should keep it in first, last or escape it if you want to put it in middle.
Also \w => [A-Za-z0-9_]
Finally, you can use this regex
[^\w.-]
Regex Demo
PHP Code
$re = "/[^\\w.-]/";
$str = "A\'skljdsg\\\sk//dgj###////#``flkl_dfd-_-sfsfs-fk kgj.docx";
$result = preg_replace($re, "", $str);
Ideone Demo
If you want to include space also, use
[^\w. -]
If You are using hypen - in middle You must escape it by slash:
preg_replace("/[^a-zA-Z0-9_\-.\s]/", "", "A\'skljdsg\\\sk//dgj###////#``flkl_dfd-_-sfsfs-fk kgj.docx");
or put it on the end:
"/[^a-zA-Z0-9_.\s-]/"

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

Removing non-alphanumeric characters from a string

I have a string in PHP and I want it to match the regex [A-Za-Z0-9]. How can I do this?
I am assuming you meant, a-z instead of a-Z, inside of your regex, but you can use preg_replace
$new_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
It takes as arguments the pattern ([a-zA-Z0-9]), replacement ("") and the subject ($string) and returns the new string ($new_string)
$string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
\W is a shortcut for [^a-Z0-9_]. May not be extremely helpful as it allows underscores too, but thought I'd let you know.

stripping out all characters from a string, leaving numbers

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.

Categories