Jump to content

removing spaces also removes CR and return characters i need kept in the string


Recommended Posts

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 this

string "hello<return>1<return> 2<return>3 <return>4 <return>"

i then use this

$string = preg_replace('/\s+/', ' ', trim($string));

and i get this

string "hello1 23 4"

but wish it to be this

string "hello<return>1<return> 2<return>3 <return>4 <return>"


does anyone know how i fix this please,

thank you for you help


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]
i see the CR or NL but thats b4 it removes the double spaces if any

this 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]
Nope, it works:

[code]<pre>
<?php
if (!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  
sakfa

AFTER:

afasfa
asj
sakfa[/code]
ah yes it does work.

but your script removes all spaces

i 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 them
if 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>
<?php
if (!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  
sakfa

AFTER:

afasfa
asj
sakfa[/code]
[/quote]
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>
<?php

if (!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]
hi thanks

i 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\n

how may i remove these doubles too. and replace with a single return of the same type.

thanks for your help.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.