dongliang1996 2015-11-13 00:14
浏览 12
已采纳

str_replace()不适用于以下情况

I would like to use str_replace() to place span elements around html strings for the purpose of highlighting them.

However the following does not work when there is   inside the string. I've tried replacing the   with ' ' but this did not help.


LIVE example

You can recreate the problem using the below code:

$str_to_replace = "as a way to incentivize more purchases.";

$replacement = "<span class='highlighter'>as a way to incentivize&nbsp;more purchases.</span>";

$subject = file_get_contents("http://venturebeat.com/2015/11/10/sources-classpass-raises-30-million-from-google-ventures-and-others/");

$output = str_replace($str_to_replace,$replacement,$subject);

.highlighter{
    background-collor: yellow;
}
  • 写回答

2条回答 默认 最新

  • douqianxian7008 2015-11-13 01:36
    关注

    So I tried your code and ran into the same problem you did. Interesting, right? The problem is that there's actually another character inbetween the "e" in "incentivize" and the " more", you can see it if you do this, split $subject into two parts, preceding the text to incentivize and after:

    // splits the webpage into two parts
    $x = explode('to incentivize', $subject);
    
    // print the char code for the first character of the second string
    // (the character right after the second e in incentivize) and also
    // print the rest of the webpage following this mystery character
    exit("keycode of invisible character: " . ord($x[1]) . " " . $x[1]);
    

    which prints: keycode of invisible character: 194 Â more ..., look! There's our mystery character, and it has charcode 194!

    Perhaps this website embeds these characters to make it difficult to do exactly what you're doing, or perhaps it's just a bug. In any case, you can use preg_replace instead of str_replace and change $str_to_replace like so:

    $str_to_replace = "/as a way to incentivize(.*?)more purchases/";
    
    $replacement = "<span class='highlighter'>as a way to incentivize more purchases.</span>";
    
    $subject = file_get_contents("http://venturebeat.com/2015/11/10/sources-classpass-raises-30-million-from-google-ventures-and-others/");
    
    $output = preg_replace($str_to_replace,$replacement,$subject);
    

    and now this does what you want. The (.*?) handles the mysterious hidden character. You can probably shrink this regular expression even further or at least cap it at a maximum amount of characters ([.]{0,5}) but in either case you likely want to stay flexible.

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

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用