andz Posted June 4, 2009 Share Posted June 4, 2009 how to remove charcters like .,!@#$%^&*()/? using str_replace and replace spaces with underscore. e.g. This is a simple, most blah, blah!!!. expected result is: this_is_a_simple_most_blah_blah Link to comment https://forums.phpfreaks.com/topic/160928-solved-str_replace/ Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 Store each one of the characters you want to remove in an array. <?php $array = array('.',',','!','@','#','$','%','^','&','*','(',')','/','?'); $str = ' some string '; $replace_string = str_replace($array, '', $str); With replacing a space with underscore, I think you can manage that. Link to comment https://forums.phpfreaks.com/topic/160928-solved-str_replace/#findComment-849280 Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 <?php $string = "This is a simple, most blah, blah!!!."; $string = str_replace(" ", "_", $string);//replcae the spaces with an underscore $remove_array(".", ",", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "/", "?"); $string = str_replace($remove_array, "", $string);//replaces the values from $remove_array with nothing echo $string; Virtually identical to what Ken gave you. Does the space to udnerscore as well. You might find a simple regular expression better suited for this. Link to comment https://forums.phpfreaks.com/topic/160928-solved-str_replace/#findComment-849282 Share on other sites More sharing options...
andz Posted June 4, 2009 Author Share Posted June 4, 2009 thanks ken and gevans for your help. well appreaciated Link to comment https://forums.phpfreaks.com/topic/160928-solved-str_replace/#findComment-849312 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.