Reputation: 7734
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
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';
Upvotes: 1
Views: 341
Reputation: 1779
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);
Upvotes: 1
Reputation: 626758
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.
Upvotes: 1