Page 1 of 1

replace in string

Posted: Mon Jun 12, 2006 10:11 am
by gurjit
hi

i want to replace all characters in a string which are not letters,numbers or a dot with a underscore

for example
http://www.mydomain.com/index.php?st=1

would be converted to
http_www.mydomain.com_index.php_st_1

Posted: Mon Jun 12, 2006 10:20 am
by Maugrim_The_Reaper
Belongs in the regex forum...but:

Code: Select all

<?php

$string = 'http://www.mydomain.com/index.php?st=1';
echo preg_replace('/[^[:alnum:].]/', '_', $string); //http___www.mydomain.com_index.php_st_1

?>