简体   繁体   中英

php preg_replace remove the last new line character?

I have following string in my database and i want to remove the "\\r\\n" at the end of the string, i tried number of times but all failed

for example, see below

<?php

$text = 'Am a 44 year old divorced lady with two adult sons. Having been single for 15years and dedicating my life to my two boys, am ready now to find love and companionship with a loving caring honest man with humane values\r\n\r\nThough 44 I look much younger. Look forward to meeting a like minded honest man and share the rest of our  journey of life together with mutual understanding and complement each other.       \r\n\r\nWill be happy to forward a photograph after initial dialogue\r\n';

echo $essay = preg_replace("/(\\r\\n)$/", ' ', $text);
echo preg_replace("/\=\r\n$/", " ", $text);

I am testing this with

http://phptester.net/

but unable to get expected output as below

$text = 'Am a 44 year old divorced lady with two adult sons. Having been single for 15years and dedicating my life to my two boys, am ready now to find love and companionship with a loving caring honest man with humane values\r\n\r\nThough 44 I look much younger. Look forward to meeting a like minded honest man and share the rest of our  journey of life together with mutual understanding and complement each other.       \r\n\r\nWill be happy to forward a photograph after initial dialogue';

The \\r\\n you want to remove are not CR+LF symbols, but a string of 4 chars because inside single quoted string literals, \\ is a literal backslash. So, your string ends with \\ , r , \\ , n . To match a literal \\ with a regex, you need to use 4 backslashes, not 2.

See the PHP demo :

$text = 'Am a 44 year old divorced lady with two adult sons. Having been single for 15years and dedicating my life to my two boys, am ready now to find love and companionship with a loving caring honest man with humane values\r\n\r\nThough 44 I look much younger. Look forward to meeting a like minded honest man and share the rest of our  journey of life together with mutual understanding and complement each other.       \r\n\r\nWill be happy to forward a photograph after initial dialogue\r\n';
echo $essay = preg_replace("/\\\\r\\\\n$/", '', $text);

Note that you may just check if a string ends with \\r\\n , and then substr it.

So First you need to change single ' to dobule quote string "

$text = "Am a 44 year old divorced lady with two adult sons. Having been single for 15years and dedicating my life to my two boys, am ready now to find love and companionship with a loving caring honest man with humane values\r\n\r\nThough 44 I look much younger. Look forward to meeting a like minded honest man and share the rest of our  journey of life together with mutual understanding and complement each other.       \r\n\r\nWill be happy to forward a photograph after initial dialogue\r\n";

then use

echo $essay  = preg_replace('/\\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM