- Forums
- PHP
- How To Remove Tabs And New Line From A String In Php
this is a simple tutorial that will show you how you remove all the tabs and the new lines from a string in a paragraph [1299], Last Updated: Sat May 18, 2024
wallpaperama
Wed May 25, 2011
0 Comments
2425 Visits
hi, i have created this short script to make you understand how you can remove a tab or a new line carriage from a string.
a new line carriage is when someone enters the enter button on their keyboard. sometimes, this key is also called the return key, but basically creates a new line in a paragraph for example.
so lets say i have an example paragraph which contains tabs and new lines and i want to shorten that string by removing all the tabs it contains and all the new lines.
so i will give you an example of what i am talking about. lets say i have this paragraph:
hi, this is line 1
this is tab 1 2 3 4
this is line 3
as you can see i have three lines and four tabs in my example above, so i would create a string like this in php:
<?php
$Foo = 'hi, this is line 1
this is tab 1 2 3 4
this is line 3';
?>
now i need to remove the tabs and the new lines, i can use the
str_replace() function in php:
<?php
$Foo = 'hi, this is line 1
this is tab 1 2 3 4
this is line 3';
$Foo = str_replace("\\n",'',$Foo);
?>
the output will look like this:
hi, this is line 1this is tab 1234this is line 3
as you can see, i removed all the tabs and new lines
here is a full script giving you examples, i tested this simple script in linux and in a window server using apache web server
PHP CODE:
<?php
#### DO NOT REMOVE ####
#### SCRIPT BY WWWW.WALLPAPERAMA.COM
#### DO NOT REMOVE ####
$Foo = 'hi, this is line 1
this is tab 1 2 3 4
this is line 3';
?>
this script removed tabs and return from a string:<br />
this is the string: $Foo
<pre><?php echo $Foo; ?></pre>
<hr />
Now i will clean the string $Foo from tabs and returns in a windows server
<strong>Windows Version</strong><pre><?php
$Foo = str_replace("\\r\\n",'',$Foo);
$Foo = str_replace("\\t",'',$Foo);
echo $Foo;
?></pre>
<hr />
Now i will clean the string $Foo from tabs and returns in a Linux server
<strong>Linux Version</strong><pre><?php
$Foo = str_replace("\\n",'',$Foo);
$Foo = str_replace("\\t",'',$Foo);
echo $Foo;
?></pre>