scarhand Posted April 7, 2009 Share Posted April 7, 2009 I need a regex that will preg_replace all "\n" (linebreaks) and "\t" (tabs) to spaces. After this is done, I want it to convert 2 spaces " " into 1 space " ". I know how to preg_replace the "\n" and "\t", but not the 2 spaces into 1. I imagine it would have something to do with {2} or something like that. Link to comment https://forums.phpfreaks.com/topic/152908-solved-convert-all-n-and-t-to-spaces-and-replace-2-spaces-with-1/ Share on other sites More sharing options...
ghostdog74 Posted April 7, 2009 Share Posted April 7, 2009 I imagine it would have something to do with {2} or something like that. you are about there. but that says match 2 spaces. if there are 2 or more spaces, you can use {2,} Link to comment https://forums.phpfreaks.com/topic/152908-solved-convert-all-n-and-t-to-spaces-and-replace-2-spaces-with-1/#findComment-803053 Share on other sites More sharing options...
fabrydesign Posted April 7, 2009 Share Posted April 7, 2009 I think what you really want to use here is trim(). Link to comment https://forums.phpfreaks.com/topic/152908-solved-convert-all-n-and-t-to-spaces-and-replace-2-spaces-with-1/#findComment-803071 Share on other sites More sharing options...
nrg_alpha Posted April 7, 2009 Share Posted April 7, 2009 fabrydesign, keep in mind that trim only eliminates the initial / trailing character(s) in question, it will not effect those deeper within the string itself. Link to comment https://forums.phpfreaks.com/topic/152908-solved-convert-all-n-and-t-to-spaces-and-replace-2-spaces-with-1/#findComment-803080 Share on other sites More sharing options...
scarhand Posted April 7, 2009 Author Share Posted April 7, 2009 I'm using this: preg_replace('[ \t\n\r]+', ' ', $string); Link to comment https://forums.phpfreaks.com/topic/152908-solved-convert-all-n-and-t-to-spaces-and-replace-2-spaces-with-1/#findComment-803633 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.