artweber Posted July 19, 2011 Share Posted July 19, 2011 Hi guys, Have to remove all symbols, double spaces from text and add comma at the end of each word. Did as follows below..but I think it can be done easiest way. Please advise. $new = preg_replace('/[^a-zA-Z0-9\s]/', '', $tags_array['title']); $new_string = preg_replace('/\s{2}/',' ', $new); $string = preg_replace('/[^a-zA-Z0-9]/', ',', $new_string); Link to comment https://forums.phpfreaks.com/topic/242294-better-performance/ Share on other sites More sharing options...
btherl Posted July 19, 2011 Share Posted July 19, 2011 Do you need to remove triple spaces as well? Because that code will only remove double spaces. If the last line is specifically to replace spaces with commas you can use str_replace() instead. Link to comment https://forums.phpfreaks.com/topic/242294-better-performance/#findComment-1244401 Share on other sites More sharing options...
artweber Posted July 19, 2011 Author Share Posted July 19, 2011 No, I don't need to remove triple spaces. As I can understand if I have to remove any dublicated spaces I can use /\s+/ instead isn't? Link to comment https://forums.phpfreaks.com/topic/242294-better-performance/#findComment-1244472 Share on other sites More sharing options...
btherl Posted July 19, 2011 Share Posted July 19, 2011 Yes, replacing '\s+' with ' ' will convert any number of whitespace (which includes spaces and some other characters) with one space, which is usually good enough. ' +' will match spaces only. Link to comment https://forums.phpfreaks.com/topic/242294-better-performance/#findComment-1244936 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.