Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 jasonc

jasonc
  • Members
  • PipPipPip
  • Advanced Member
  • 841 posts

Posted 02 June 2006 - 03:14 PM

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




#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 03:25 PM

There is a small problem with the regex: It will not remove spaces. Change it from:
$strPattern = "/[^a-zA-Z0-9\r\n ]/";
To
$strPattern = "/[^a-zA-Z0-9\r\n]/";

(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]
~ D Kuang

#3 jasonc

jasonc
  • Members
  • PipPipPip
  • Advanced Member
  • 841 posts

Posted 02 June 2006 - 03:33 PM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
There is a small problem with the regex: It will not remove spaces. Change it from:
$strPattern = "/[^a-zA-Z0-9\r\n ]/";
To
$strPattern = "/[^a-zA-Z0-9\r\n]/";

(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]


#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 03:42 PM

Nope, it works:

<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>

BEFORE: 

afasfa$@*
  asj   
sakfa

AFTER: 

afasfa
asj
sakfa

~ D Kuang

#5 jasonc

jasonc
  • Members
  • PipPipPip
  • Advanced Member
  • 841 posts

Posted 02 June 2006 - 03:58 PM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
Nope, it works:

<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>

BEFORE: 

afasfa$@*
  asj   
sakfa

AFTER: 

afasfa
asj
sakfa
[/quote]


#6 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 04:39 PM

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.
<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>

~ D Kuang

#7 jasonc

jasonc
  • Members
  • PipPipPip
  • Advanced Member
  • 841 posts

Posted 03 June 2006 - 09:21 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users