All non alphanumeric but also allow _
Posted: Wed Mar 08, 2006 10:54 am
Just as the title states, I need to strip out everything that is not alphanumeric but I need to allow the _ character through. How do I do this?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$data = preg_replace('#[^a-z_]+#i', '', $data);Code: Select all
preg_replace('/\W+/', '', $string);Code: Select all
$data = preg_replace('#[^a-z0-9_]+#', '', $data);Code: Select all
$data = preg_replace('#[^a-z0-9 _]+#', '', $data);Code: Select all
$data = preg_replace('#[^a-z0-9;_]+#', '', $data);Code: Select all
$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);Or even easier:feyd wrote:my brain must be off today, I forgot the i modifier for the pattern in the updated one.
with spaceCode: Select all
$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);
You're mistaken..d11wtq wrote:Or even easier:feyd wrote:my brain must be off today, I forgot the i modifier for the pattern in the updated one.
with spaceCode: Select all
$data = preg_replace('#[^a-z0-9 _]+#i', '', $data);
#[^\w ]+#
No "i" neededI never get why people use a-z0-9_ when that's precisely what \w is
Code: Select all
<?php
$str = '';
for($i = 0; $i < 256; $i++)
{
$str .= chr($i);
}
echo preg_replace('#[^\w]#', '', $str);
?>Code: Select all
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzâèîÄÜ£₧ƒ¬▓│╡╣║└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷°∙·√ⁿ²■