duanpi7578 2017-06-07 10:15
浏览 102

在str_replace中排除html属性

so i'm trying to do the usual replacing each instance of a company name in wordpress, and i've run into an issue where on occasion (usually in the href of links) it's breaking links.

code i'm using (includes a filter for ACF custom fields)

    //Replace the word lunch with a span
function replace_text_wps($text){
    $replace = array(
        // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
        'lunch!' => '<span class="lunch">lunch!</span>',
        'Lunch!' => '<span class="lunch">lunch!</span>',
        'LUNCH!' => '<span class="lunch">lunch!</span>'
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
}

add_filter('the_content', 'replace_text_wps');
add_filter('the_excerpt', 'replace_text_wps');
add_filter('the_title', 'replace_text_wps');
add_filter('acf_the_content', 'replace_text_wps');

Is there a way to only replace it outside of stuff like href tags? i've looked into stuff like DOM parses but i'm not sure if there's a simpler method- it doesn't seem to cause grief in any other instances so i'm not sure that pre=processing the entire HMTL and looking for the word is appropriate. I did wonder about searching for the string including a space before and after it, but i wasn't sure how to include leading / trailing spaces, and it felt a bit bodge-y? Thanks in advance! Pete

  • 写回答

1条回答 默认 最新

  • dow98764 2017-06-07 11:11
    关注

    ok, so in this instance, which is quite specific, i've found the following code works well enough. added trailing / leading spaces in the initial matching parameters, and also variants to find it directly after html tags including with a space. less elegant, but sufficient for the purpose here.

    function replace_text_wps($text){
        $replace = array(
            // used mid-line
            ' lunch! ' => ' <span class="lunch">lunch!</span> ',
            ' Lunch! ' => ' <span class="lunch">lunch!</span> ',
            ' LUNCH! ' => ' <span class="lunch">lunch!</span> ',
            // used at end of lines
            ' lunch!' => ' <span class="lunch">lunch!</span>',
            ' Lunch!' => ' <span class="lunch">lunch!</span>',
            ' LUNCH!' => ' <span class="lunch">lunch!</span>',
            // used inside html tags like headers
            '>lunch!' => '><span class="lunch">lunch!</span>',
            '>Lunch!' => '><span class="lunch">lunch!</span>',
            '>LUNCH!' => '><span class="lunch">lunch!</span>',
            //used directly after html tags
            '> lunch!' => '> <span class="lunch">lunch!</span>',
            '> Lunch!' => '> <span class="lunch">lunch!</span>',
            '> LUNCH!' => '> <span class="lunch">lunch!</span>',
            //exclude alt tags on images, title attributes etc
            '"lunch!' => '"lunch!',
            'lunch!"' => 'lunch!"',
            '"Lunch!' => '"lunch!',
            'Lunch!"' => 'lunch!"'
        );
        $text = str_replace(array_keys($replace), $replace, $text);
        return $text;
    }
    add_filter('the_content', 'replace_text_wps');
    add_filter('the_excerpt', 'replace_text_wps');
    add_filter('the_title', 'replace_text_wps');
    add_filter('acf_the_content', 'replace_text_wps');
    
    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False