ballhogjoni Posted August 22, 2008 Share Posted August 22, 2008 I have a string "Crumbling Credit Economy: What should YOU do?" and I want to replace the spaces with a hyphen(-) and get rid of all the other characters except the numbers and letters. How would I do that? code so far: $sTitleUrl = ereg_replace("[^A-Za-z0-9]", "", str_replace(" ", "-", $sTitle)); Quote Link to comment https://forums.phpfreaks.com/topic/120934-solved-ereg_replace-question/ Share on other sites More sharing options...
JasonLewis Posted August 23, 2008 Share Posted August 23, 2008 Should be in Regular Expressions section but it doesn't matter. <?php $str = "Crumbling Credit Economy: What should YOU do?"; //Remove all unwanted characters, keeping spaces as well! $str = preg_replace("#[^a-zA-Z0-9 ]+#s", "", $str); //Now add in hyphens for spaces $str = str_replace(" ", "-", $str); //One Line? $oneLine = preg_replace("#[^a-zA-Z0-9\-]+#s", "", str_replace(" ", "-", $str)); echo $str; ?> The one line solution is okay, but we are allowing hyphens in the expression. Quote Link to comment https://forums.phpfreaks.com/topic/120934-solved-ereg_replace-question/#findComment-623444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.