Thanks
Replacing non alphanumeric chars in a string
Moderator: General Moderators
Replacing non alphanumeric chars in a string
How would I remove any non-alphanumeric characters that could be present in $var?
Thanks
Thanks
there is also a class [:alnum:] defined for PCREwill do virtually the same as the example above
Code: Select all
<?php
$subject = 'abc&%$_123';
echo preg_replace('![^[]]!', '', $subject);
?>oh ok .. cool
what's woth the exclaination marks on either side ? I haven't seen that before
what's woth the exclaination marks on either side ? I haven't seen that before
Code: Select all
!ї^ї:alnum:]]!the preg_xyz functions of php are an approximation to perl's regex syntax where one can use something likeperl recognizes the expression between // and so do the preg_-functions, but you may choose the delimiting character (almost) freely in php. So !! is the same as //. I only use it because my expressions contain far less often an ! than / 
(but was astonished the first time I saw it, too =] )
Code: Select all
if ($s =~ /^\d+(.*)/i)
{ print $1; }
# php
# if (preg_match('/^\d+(.*)/i', $s, $matches))
# echo $matchesї1];(but was astonished the first time I saw it, too =] )