adrianTNT Posted January 20, 2011 Share Posted January 20, 2011 Can someone tell me how to convert this ereg_replace to preg_replace ? $string = ereg_replace("[^A-Za-z0-9_]", " ", $string); It is supposed to only allow letters and numbers. Then I also need to replace multiple spaces that was like this: $string = ereg_replace("( )+", "_", $string); Link to comment https://forums.phpfreaks.com/topic/225115-converting-a-ereg_replace-to-preg_replace/ Share on other sites More sharing options...
gizmola Posted January 20, 2011 Share Posted January 20, 2011 The main difference is that the preg_ functions need a delimitter around your regex. Otherwise, the internal regex (if it worked before) will work fine with preg_ equivalents although you might need to read up on the parameters and return values. Link to comment https://forums.phpfreaks.com/topic/225115-converting-a-ereg_replace-to-preg_replace/#findComment-1162715 Share on other sites More sharing options...
adrianTNT Posted January 20, 2011 Author Share Posted January 20, 2011 Thanks, so I tried this, and it seems to do the trick, (if anyone sees a problem please let me know): $string = preg_replace("/[^A-Za-z0-9_]/", " ",$string); Link to comment https://forums.phpfreaks.com/topic/225115-converting-a-ereg_replace-to-preg_replace/#findComment-1162718 Share on other sites More sharing options...
salathe Posted January 21, 2011 Share Posted January 21, 2011 ... the internal regex (if it worked before) will work fine with preg_ equivalents ... ... will usually work fine... For reference to some differences, see http://php.net/reference.pcre.pattern.posix Link to comment https://forums.phpfreaks.com/topic/225115-converting-a-ereg_replace-to-preg_replace/#findComment-1162883 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.