replace in string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

replace in string

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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

?>
Post Reply