dongyi1490 2014-08-29 06:06
浏览 41
已采纳

PHP preg_replace 

I'm stuck with this one...

I insert some data into a database table which comes from a form. Sometimes, this data contains html, so I do

$note = htmlentities(mysqli_real_escape_string($this->db, $_POST['note']));

I then store the $note in my database. When retrieving my notes, I check whether there is html code in it, and if there is, I want to have it highlighted using highlight_string(). That part works like a charm. Now here's the problem:

I am displaying the highlighted string in a div container, but it's overflowing because the white spaces are being converted to  . I'm trying to remove those non-breaking spaces and replace them with a simple space so the highlighted string will be contained in the div. I have tried this so far:

$note = html_entity_decode($note);
$note = highlight_string($note);
$note = preg_replace('/ /', '', $note);  

I have also tried $note = str_replace(' ', ' ', $note);, to no avail.

Any help is very much appreciated! Thanks :)

  • 写回答

2条回答 默认 最新

  • douzi9211 2014-08-29 06:14
    关注

    I think the problem is quite simply that highlight_string() is outputting its result immediately, rather than saving it to $note.

    Instead, please try the following:

    $note = html_entity_decode($note);
    $note = highlight_string($note, true);
    $note = str_replace(' ', ' ', $note);
    

    The difference in my code is that I use highlight_string($note, true) with the second parameter set to true. The docs shed some light about the function's behavior:

    mixed highlight_string ( string $str [, bool $return = false ] )

    Return
     Set this parameter to TRUE to make this function return the highlighted code.

    The regex function you have in your code block might work, but since this is a simple replacement, it will suffice to use str_replace in this case, as you have tried.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测