jasonc Posted June 2, 2006 Share Posted June 2, 2006 i have a string that contains return characters, new lines, carrige returns or what every you want to call them.i will use <return> for any type of return it might be.string "hello^<return>1^<return> 2$<return>3 (<return>4 '<return>"i use the following to remove the spaces but it also removes all the returns or CR from the string too.$strPattern = "/[^a-zA-Z0-9\r\n ]/";$string = preg_replace($strPattern, "", $string);and i get thisstring "hello<return>1<return> 2<return>3 <return>4 <return>"i then use this$string = preg_replace('/\s+/', ' ', trim($string));and i get thisstring "hello1 23 4"but wish it to be thisstring "hello<return>1<return> 2<return>3 <return>4 <return>"does anyone know how i fix this please,thank you for you help Quote Link to comment https://forums.phpfreaks.com/topic/11035-removing-spaces-also-removes-cr-and-return-characters-i-need-kept-in-the-string/ Share on other sites More sharing options...
poirot Posted June 2, 2006 Share Posted June 2, 2006 There is a small problem with the regex: It will not remove spaces. Change it from:[code]$strPattern = "/[^a-zA-Z0-9\r\n ]/";[/code]To[code]$strPattern = "/[^a-zA-Z0-9\r\n]/";[/code](The space was included in the exclusion)Anyways, it should be already working. I guess you can't see the returns because it's output in HTML.To fix this, you can a <pre> tag or use nl2br():[a href=\"http://www.php.net/nl2br\" target=\"_blank\"]http://www.php.net/nl2br[/a] Quote Link to comment https://forums.phpfreaks.com/topic/11035-removing-spaces-also-removes-cr-and-return-characters-i-need-kept-in-the-string/#findComment-41204 Share on other sites More sharing options...
jasonc Posted June 2, 2006 Author Share Posted June 2, 2006 i see the CR or NL but thats b4 it removes the double spaces if anythis is what i have as an example page.<?if ($_POST) {echo('.'.$_POST[textarea].'.<br>');$strPattern = "/[^a-zA-Z0-9\r\n ]/";$_POST[textarea] = preg_replace($strPattern, "", $_POST[textarea]);$_POST[textarea] = preg_replace('/\s+/', ' ', trim($_POST[textarea]));echo('.'.$_POST[textarea].'.');}?><form name="form1" method="post" action=""> <p> <textarea name="textarea" rows="10"><? echo($_POST[textarea]);?></textarea> <input type="submit" name="Submit" value="Submit"> </p></form>[!--quoteo(post=379367:date=Jun 2 2006, 04:25 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 2 2006, 04:25 PM) [snapback]379367[/snapback][/div][div class=\'quotemain\'][!--quotec--]There is a small problem with the regex: It will not remove spaces. Change it from:[code]$strPattern = "/[^a-zA-Z0-9\r\n ]/";[/code]To[code]$strPattern = "/[^a-zA-Z0-9\r\n]/";[/code](The space was included in the exclusion)Anyways, it should be already working. I guess you can't see the returns because it's output in HTML.To fix this, you can a <pre> tag or use nl2br():[a href=\"http://www.php.net/nl2br\" target=\"_blank\"]http://www.php.net/nl2br[/a][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11035-removing-spaces-also-removes-cr-and-return-characters-i-need-kept-in-the-string/#findComment-41207 Share on other sites More sharing options...
poirot Posted June 2, 2006 Share Posted June 2, 2006 Nope, it works:[code]<pre><?phpif (!empty($_POST['textarea'])) { echo 'BEFORE: ' . "\r\n\r\n" . $_POST['textarea'] . "\r\n\r\n"; $strPattern = "/[^a-zA-Z0-9\r\n]/"; $formated = preg_replace($strPattern, "", $_POST['textarea']); echo 'AFTER: ' . "\r\n\r\n" . $formated;}?><form name="form1" method="post" action=""><p><textarea name="textarea" rows="10"><? echo($_POST['textarea']);?></textarea><input type="submit" name="Submit" value="Submit"></p></form>[/code][code]BEFORE: afasfa$@* asj sakfaAFTER: afasfaasjsakfa[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11035-removing-spaces-also-removes-cr-and-return-characters-i-need-kept-in-the-string/#findComment-41210 Share on other sites More sharing options...
jasonc Posted June 2, 2006 Author Share Posted June 2, 2006 ah yes it does work.but your script removes all spacesi would like to remove only spaces where their are more than one in any section of the script,so if their are 2 then it removes one of themif their are 3 then it removes two, so there is only one space and not many between the letters.[!--quoteo(post=379373:date=Jun 2 2006, 04:42 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 2 2006, 04:42 PM) [snapback]379373[/snapback][/div][div class=\'quotemain\'][!--quotec--]Nope, it works:[code]<pre><?phpif (!empty($_POST['textarea'])) { echo 'BEFORE: ' . "\r\n\r\n" . $_POST['textarea'] . "\r\n\r\n"; $strPattern = "/[^a-zA-Z0-9\r\n]/"; $formated = preg_replace($strPattern, "", $_POST['textarea']); echo 'AFTER: ' . "\r\n\r\n" . $formated;}?><form name="form1" method="post" action=""><p><textarea name="textarea" rows="10"><? echo($_POST['textarea']);?></textarea><input type="submit" name="Submit" value="Submit"></p></form>[/code][code]BEFORE: afasfa$@* asj sakfaAFTER: afasfaasjsakfa[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11035-removing-spaces-also-removes-cr-and-return-characters-i-need-kept-in-the-string/#findComment-41214 Share on other sites More sharing options...
poirot Posted June 2, 2006 Share Posted June 2, 2006 Oh, I understand now.Actually what the other script they gave you failed to do was to not remove the returns. This happened because preg_replace('/\s+/', ' ', $str) which is supposed to strip extra white spaces will strip returns too (as they are considered whitespace characters)This one should do it.[code]<pre><?phpif (!empty($_POST['textarea'])) { echo 'BEFORE: ' . "\r\n\r\n" . $_POST['textarea'] . "\r\n\r\n"; $strPattern = "/[^a-zA-Z0-9\r\n ]/"; $formated = preg_replace($strPattern, "", $_POST['textarea']); $formated = preg_replace("/ {2,}/", " ", $formated); echo 'AFTER: ' . "\r\n\r\n" . $formated;}?><form name="form1" method="post" action=""><p><textarea name="textarea" rows="10"><? echo($_POST['textarea']);?></textarea><input type="submit" name="Submit" value="Submit"></p></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11035-removing-spaces-also-removes-cr-and-return-characters-i-need-kept-in-the-string/#findComment-41229 Share on other sites More sharing options...
jasonc Posted June 3, 2006 Author Share Posted June 3, 2006 hi thanksi also need the double returns replaced with single returns, like with the space.i have been told that their are three types of return characters.\r \n \r\nhow may i remove these doubles too. and replace with a single return of the same type.thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/11035-removing-spaces-also-removes-cr-and-return-characters-i-need-kept-in-the-string/#findComment-41412 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.