douluo5937 2011-07-01 06:31
浏览 48
已采纳

如果我使用nl2br(),为什么会出现 ?

Someone just made a group on my site which has a description with . The thing is that I already do this to the description text:

//Convert all urls to links
$group_description = preg_replace('#([\s|^])(www)#i', '$1http://$2', $group_description);
$pattern = '#((http|https|ftp|telnet|news|gopher|file|wais):\/\/[^\s]+)#i';
$replacement = '<a href="$1" target="_blank">$1</a>';
$group_description = preg_replace($pattern, $replacement, $group_description);

$group_description = str_replace("\'" , "'", $group_description );
$group_description = nl2br($group_description);

/* Convert all E-mail matches to appropriate HTML links */
$pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
$pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
$replacement = '<a href="mailto:\\1">\\1</a>';
$group_description = preg_replace($pattern, $replacement, $group_description);

You can see this happening here.

You see it says there Welcome to Join.

Why didn't it become a <br /> tag?

This is probably important, here is how I put this text into the db in the first place:

$group_description = mysql_real_escape_string($_POST['group_description']);
// And then insert the $group_description into the db without doing any more operations on it.

Any idea why I am still getting the ?

  • 写回答

2条回答 默认 最新

  • dongshadu4498 2011-07-01 06:36
    关注

    The problem is that the and are stored in your database as "\" "r" and "\" " " respectively. They're not stored as a carriage return or new line character but the slash character followed by the letter. When you're calling nl2br(), it's looking for the new line character, not the textual representation of it. Hopefully that makes sense.

    You could use a regular expression (preg_replace) or str_replace to replace them. But the problem probably is caused by how the data is inserted in your database.

    For instance:

    $string = str_replace('
    ', "
    ", $string);
    $string = str_replace('', "", $string);
    

    You'll notice that I used single quotes for the first one and double for the second. When using single quotes, the "" and " " are stored as text, not the special characters. That could by why they're not stored properly in your database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图