Remove chars

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Remove chars

Post by JKM »

Hi there,

what is the best way to remove all chars but:[text]1-9a-Z|-_.,#%(){}[]~^+?![/text]
If regex is the way to go, could anyone please give me the regex sentence I should use?

Thanks!
Merry xmas!
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Remove chars

Post by Darhazer »

Try this one:

Code: Select all

$charset = '1-9a-zA-Z\|\-\_\.\,\#\%\(\)\{\}\[\]\~\^\+\?\!';
$string = '@12345678900-=+_aAbBcC';
$result = preg_replace('/[^'.$charset.']/i', '', $string);
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Remove chars

Post by s.dot »

You don't need regular expressions, but it will work.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply