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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line