I am trying to make a function that will make a string more web-friendly. I want the result to be suitable for a URL, so no spaces or special characters are allowed, ie "Richard's Website" will become "richards_website".

I finished all the steps except the last one because i am not familiar with regular expressions. The function below is what I have so far, with the missing step. Can someone show me how to write a regular expression that will delete all non-alphanumerics?

function makefriendlyname ($text) {
//returns a web-friendly version of any string passed
$text=str_replace(" ","_",$text); //replace spaces
//now make lower case...
$text = strtolower($text);
//now delete all punctuation (anything that is not a number or lower case letter)...

return $text;

}

    Definitely check out the links on Regular Expressions and take some time to learn about them. They can prove invaluable in many instances where you might need to parse, scrub or format data.

    As an alternative you can add the following line to your existing function between the strtolower() call and the return $text:

    // Replace any character not a lower case letter, number or underscore
    $text = preg_replace("/[a-z0-9_]/","",$text);

      Write a Reply...