PHP Remove extra spaces between break lines - php

I have a string in PHP, i'm able to remove multiple continuous break lines and multiple spaces, but what i'm still not able is to remove multiple break lines if i have an space in the middle.
For example:
Text \r\n \r\n extra text
I would like to clean this text as:
Text \r\nextra text
Could also be too:
Text \r\n \r\nextra text
Don't need to be an extra espace after the break line.
What i have right now is:
function clearHTML($text){
$text = strip_tags($text);
$text = str_replace(" ", " ", $text);
$text = preg_replace("/[[:blank:]]+/"," ",$text);
$text = preg_replace("/([\r\n]{4,}|[\n]{2,}|[\r]{2,})/", "\r\n", $text);
$text = trim($text);
return $text;
}
Any suggestions?

To remove extra whitespace between lines, you can use
preg_replace('~\h*(\R)\s*~', '$1', $text)
The regex matches:
\h* - 0 or more horizontal whitespaces
(\R) - Group 1: any line ending sequence (the replacement is $1, just this group vaue)
\s* - one or more whitespaces
The whitespace shrinking part can be merged to a single preg_replace call with the (?: |\h)+ regex that matches one or more occurrences of an string or a horizontal whitespace.
NOTE: If you have Unicode texts, you will need u flag.
The whole cleaning function can look like
function clearHTML($text){
$text = strip_tags($text);
$text = preg_replace("~(?: |\h)+~u", " ", $text);
$text = preg_replace('~\h*(\R)\s*~u', '$1', $text);
return trim($text);
}

Related

Break Long Strings Of Text That Don't Contain A Space

How can I put spaces to a long string that does not have spaces
Example : 5Bedroom.Apartment,in.NewYork>City
I want to put spaces after any dot and comma. Only if no space after dot and comma. If already have space, just ignore
you should replace the charector which u want
$str = preg_replace('/(?<!\d),|,(?!\d{3})/', ', ', $str);
Such regex ~(?<=[,.])(?=\S)~ matches position after comma or dot before not space
$str = preg_replace( ~(?<=[,.])(?=\S)~, " ", $str);
demo

Text file as single string in PHP code

my text file is like this:
atagatatagatagtacataacta\n
actatgctgtctgctacgtccgta\n
ctgatagctgctcgctactacgat\n
gtcatgatctgatctacgatcaga\n
I need this file in single string or in single line in both same and reverese order like this:
atagatatagatagtacataactaactatgctgtctgctacgtccgtactgatagctgctcgctactacgatgtcatgatctgatctacgatcaga
and "reverese" (for which I didn't write code because I need help ).
I am using:
<?php
$re = "/[AG]?[AT][AT]GAGG[ATC]GC[GA]?[ATGC]/";
$str = file_get_contents("filename.txt");
trim($str);
preg_match($re, $str, $matches);
print_r($matches);
?>
You can remove spaces and newlines using preg_replace, and you can reverse a string using strrev.
$yourString = "atagatatagatagtacataacta\n actatgctgtctgctacgtccgta\n ctgatagctgctcgctactacgat\n gtcatgatctgatctacgatcaga\n";
$stringWithoutSpaces = preg_replace("/\s+/", "", $yourString);
$stringReversed = strrev($stringWithoutSpaces);
echo $stringReversed;
http://php.net/manual/de/function.preg-replace.php
http://php.net/manual/en/function.strrev.php
Explanation:
With preg_replace you replace any character in $yourString with an empty string "" that matches the search pattern "/\s+/". The \s in the search pattern stands for any whitespace character (tab, linefeed, carriage return, space, formfeed), the + is there to match also multiple whitespace characters, not just one.

Remove white space from first line of textarea

how can i prevent php to remove the whitespace from the begining of the textarea first line. Everytime i submited the form, the withespace are removed...even if i replace it for nbsp;
The code i'm using:
PHP
if(isset($_POST['btn_cel'])){
$text = trim($_POST['cel']);
$text = explode("\n", $text);
foreach($texto as $line){
echo str_replace(' ',' ',$line);
}
}
The input
1234 5678
abcdefghif
The output
1234 5678
abcdefghif
Get rid of the call to trim.
This function returns a string with whitespace stripped from the beginning and end of str.
From the PHP documentation :
trim — Strip whitespace (or other characters) from the beginning and end of a string
You should not be using the trim() function if you want php to leave whitespaces as it is. If you apply trim on some string it removes whitespaces from the beginning and end of it.
if(isset($_POST['btn_cel'])){
$text = trim($_POST['cel']);
$text = explode("\n", $text);
foreach($texto as $line){
echo trim(str_replace(' ',' ',$line));
}
}

Php display as a html text the new line \n

I'm using echo to display result but the result contains line breaks /n /t /r.
I want to know if the result has is \n or \t or \r and how many. I need to know so I can replace it in a html tag like <p> or <div>.
The result is coming from on other website.
In pattern CreditTransaction/CustomerData:
Email does not contain any text
In pattern RecurUpdate/CustomerData:
Email does not contain any text
In pattern AccountInfo:
I want like this.
In pattern CreditTransaction/CustomerData:
\n
\n
\n
\n\tEmail does not contain any text
\n
In pattern RecurUpdate/CustomerData:
\n
\n
\n
\n\tEmail does not contain any text
\n\tIn pattern AccountInfo:
Your question is quite unclear but I'll do my best to provide an answer.
If you want to make \n, \r, and \t visible in the output you could just manually unescape them:
str_replace("\n", '\n', str_replace("\r", '\r', str_replace("\t", '\t', $string)));
Or if you want to unescape all escaped characters:
addslashes($string);
To count how many times a specific character/substring occurs:
substr_count($string, $character_or_substring);
To check if the string contains a specific character/substring:
if (substr_count($string, $character_or_substring) > 0) {
// your code
}
Or:
if (strpos($string, $character_or_substring) !== false) { // notice the !==
// your code
}
As mentioned earlier by someone else in a comment, if you want to convert the newlines to br tags:
nl2br($string);
If you want to make tabs indenting you could replace all tabs with  :
str_replace("\t", ' ', $string);
Use double quotes to find newline and tab characters.
$s = "In pattern CreditTransaction/CustomerData:
Email does not contain any text
In pattern RecurUpdate/CustomerData: ";
echo str_replace("\t", "*", $s); // Replace all tabs with '*'
echo str_replace("\n", "*", $s); // Replace all newlines with '*'

Removing various sorts of whitespace in PHP

I am looking to remove multiple line breaks using regular expression. Say I have this text:
"On the Insert tab\n \n\nthe galleries include \n\n items that are designed"
then I want to replace it with
"On the Insert tab\nthe galleries include\nitems that are designed"
So my requirement is:
it will remove all multiple newlines and will replace with one newline
It will remove all multiple spaces and will replace with one space
Spaces will be trimmed as well
I do searched a lot but couldn't find solution - the closest I got was this one Removing redundant line breaks with regular expressions.
Use this :
echo trim(preg_replace('#(\s)+#',"$1",$string));
$text = str_replace("\r\n", "\n", $text); // converts Windows new lines to Linux ones
while (strpos($text, "\n\n") != false)
{
$text = str_replace("\n\n", "\n", $text);
}
That will sort out newline characters.
$text = trim($text);
preg_replace('/\s+/', ' ', $text);
preg_replace('/(?:\s*(?:\r\n|\r|\n)\s*){2}/s', "\n", $text);
Thanks to Removing redundant line breaks with regular expressions

Categories