duanduoding2238 2013-02-06 20:06
浏览 42
已采纳

str_replace不适用于斜杠

For some reason str_replace() does not work with /. I am creating a function to accept a unique linking style in input and text area forms for a blog CMS that I am making. For instance, [{http://brannondorsey.com}My Website] will be translated to <a href='http://brannondorsey.com'>My Website</a> when passed through make_link($string);. Here is my code:

function make_link($input){

 $double = str_replace( '"', '&#34', $input);
 $single = str_replace("'", "&#39", $double);
 $bracket_erase = str_replace('[', "", $single);
 $link_open = str_replace('{', '<a href="', $bracket_erase);
 $link_close = str_replace("}", ">", $link_open);
 $link_value = str_replace(']', "</a>", $link_close);

 echo $link_value;
 }

Everything works correctly except for ] is not replaced with </a>. If I remove the slash it will successfully replace ] with <a>, however, as we all know, that does not properly close an anchor tag and therefor makes all html content between the {and the next closing anchor tag in my webpage a link.

  • 写回答

2条回答 默认 最新

  • duankua3620 2013-02-06 20:15
    关注

    I personally suggest the preg_replace answer of Marcus Recck below rather than mine here.

    its there just not seen because the browser wont show html, but you can use the below to see it, and\or use the browsers view source option

    $link_close ="]";
    
    $link_value = str_replace(']', "</a>", $link_close);
    
    echo htmlspecialchars($link_value);//= &lt;/a&gt;
    
    var_dump ($link_value); //=string(4) "" [invisible due to browser, but the 4 tells you its there]
    

    the finial version of the OP's function:

    function make_link($input){
    
     $double = str_replace( '"', '&#34', $input);
     $single = str_replace("'", "&#39", $double);
     $bracket_erase = str_replace('[', "", $single);
     $link_open = str_replace('{', '<a href="', $bracket_erase);
     $link_close = str_replace("}", '">', $link_open);
     $link_value = str_replace(']', "</a>", $link_close);
    
     return $link_value;
     }
    echo htmlspecialchars(make_link('[{http://brannondorsey.com}My Website]'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)