php preg_replace 删除最后一个换行符?
php preg_replace remove the last new line character?
我的数据库中有以下字符串,我想删除字符串末尾的“\r\n”,我尝试了很多次,但都失败了
例如,见下文
<?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);
我正在用
测试这个
但无法获得如下所示的预期输出
$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
不是 CR+LF 符号,而是一个 4 个字符的字符串,因为在单引号字符串文字中,\
是文字反斜杠。因此,您的字符串以 \
、r
、\
、n
结尾。要将文字 \
与正则表达式匹配,您需要使用 4 个反斜杠,而不是 2 个。
参见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);
请注意,您可以只检查字符串是否以 \r\n
结尾,然后再检查 substr
。
所以首先你需要把单引号'
改成双引号字符串"
$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);
我的数据库中有以下字符串,我想删除字符串末尾的“\r\n”,我尝试了很多次,但都失败了
例如,见下文
<?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);
我正在用
测试这个但无法获得如下所示的预期输出
$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
不是 CR+LF 符号,而是一个 4 个字符的字符串,因为在单引号字符串文字中,\
是文字反斜杠。因此,您的字符串以 \
、r
、\
、n
结尾。要将文字 \
与正则表达式匹配,您需要使用 4 个反斜杠,而不是 2 个。
参见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);
请注意,您可以只检查字符串是否以 \r\n
结尾,然后再检查 substr
。
所以首先你需要把单引号'
改成双引号字符串"
$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);