Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all non-matching characters in PHP string?

Tags:

regex

php

I've got text from which I want to remove all characters that ARE NOT the following.

desired_characters =

0123456789!&',-./abcdefghijklmnopqrstuvwxyz\n

The last is a \n (newline) that I do want to keep.

like image 593
siliconpi Avatar asked Oct 03 '10 10:10

siliconpi


2 Answers

$string = 'This is anexample $tring! :)';
$string = preg_replace('/[^0-9!&\',\-.\/a-z\n]/', '', $string);

echo $string; // hisisanexampletring!

^ This is case sensitive, hence the capital T is removed from the string. To allow capital letters as well, $string = preg_replace('/[^0-9!&\',\-.\/A-Za-z\n]/', '', $string)

like image 34
chigley Avatar answered Sep 28 '22 00:09

chigley


To match all characters except the listed ones, use an inverted character set [^…]:

$chars = "0123456789!&',-./abcdefghijklmnopqrstuvwxyz\n";
$pattern = "/[^".preg_quote($chars, "/")."]/";

Here preg_quote is used to escape certain special characters so that they are interpreted as literal characters.

You could also use character ranges to express the listed characters:

$pattern = "/[^0-9!&',-.\\/a-z\n]/";

In this case it doesn’t matter if the literal - in ,-. is escaped or not. Because ,-. is interpreted as character range from , (0x2C) to . (0x2E) that already contains the - (0x2D) in between.

Then you can remove those characters that are matched with preg_replace:

$output = preg_replace($pattern, "", $str);
like image 122
Gumbo Avatar answered Sep 28 '22 02:09

Gumbo