I want to replace multiple Newline characters with one Newline character, multiple spaces with a single space and Insert In to database.
so far I tried this preg_replace("/\\n\\n+/", "\\n", $text);
and doesn't work !
I also do this job on the $text for formatting.
$text = wordwrap($text,120, '<br/>', true);
$text = nl2br($text);
Try using the following pattern:
/[\n\r]+/
as follows:
preg_replace( "/[\r\n]+/", "\n", $text );
You probably need this:
preg_replace("/(\s)+/", "$1", $input_lines);
\\s --- matches any white space character (all characters like spaces, tabs, new lines, etc)
$1 --- The first white-space char in a set. If the first is a space and the we have 3 new lines after it. We'll get only 1 space.
Alternatively you can use this:
preg_replace("/(\n)+/", "$1", $input_lines);
preg_replace("/( )+/", "$1", $input_lines);
尝试使用以下内容:
$text = str_replace(PHP_EOL, '', $text);
You need to use the correct end of line character depending on the system. PHP_EOL
determines the end of line character for you.
$text = str_replace(PHP_EOL, array("\\n", "\\r\\n", "\\r"), $text);
<br />
is for HTML only
Windows uses "\\r\\n" for new line characters
Unix-based uses "\\n"
Mac (I think) uses "\\r"
尝试preg_replace()
:
preg_replace("/[\r\n]+/", "\n", $text);
Considering the spaces in the last line. $text= preg_replace( "/[\\r\\n]+\\s+/", "\\r\\n", $text );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.