排除 str_replace 中的 html 属性 [英] exclude html attributes in str_replace
问题描述
所以我试图像往常一样替换 wordpress 中公司名称的每个实例,但我遇到了一个问题,有时(通常在链接的 href 中)它会破坏链接.
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.
我正在使用的代码(包括 ACF 自定义字段的过滤器)
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');
有没有办法只在 href 标签之类的东西之外替换它?我已经研究过像 DOM 解析这样的东西,但我不确定是否有更简单的方法 - 它似乎不会在任何其他情况下引起悲伤,所以我不确定预处理整个 HMTL 并寻找词是合适的.我确实想知道搜索包含前后空格的字符串,但我不确定如何包含前导/尾随空格,感觉有点笨拙?提前致谢!皮特
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
推荐答案
好的,所以在这个非常具体的例子中,我发现下面的代码工作得很好.在初始匹配参数中添加尾随/前导空格,以及在包含空格的 html 标签之后直接找到它的变体.不太优雅,但足以达到这里的目的.
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');
这篇关于排除 str_replace 中的 html 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!