Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Wordpress删除内容粗体的文本_Php_Wordpress - Fatal编程技术网

Php 使用Wordpress删除内容粗体的文本

Php 使用Wordpress删除内容粗体的文本,php,wordpress,Php,Wordpress,我使用下面的代码从一个类别中输出内容,但是内容有粗体标记,这反过来使我的整个内容都是粗体的。删除代码中粗体文本的最简单方法是什么?任何帮助都将不胜感激,因为我正在用它来学习 <p><?php $content = get_the_content(); if (mb_strlen($content) > 700) { $content = mb_substr($content, 0, 700);

我使用下面的代码从一个类别中输出内容,但是内容有粗体标记,这反过来使我的整个内容都是粗体的。删除代码中粗体文本的最简单方法是什么?任何帮助都将不胜感激,因为我正在用它来学习

<p><?php $content = get_the_content();  
                if (mb_strlen($content) > 700) {
                $content = mb_substr($content, 0, 700);
                // make sure it ends in a word by chomping at last space
                $content = mb_substr($content, 0, mb_strrpos($content, " ")).'...<br /><span class="landing_latest_articles_read_more"><a href="" title="">Read More</a></span>';
                }
                echo $content; ?></p>

或者这可能有用

$string = preg_replace("/<b>|</b>/", "", $string);
$string=preg_replace(“/|/”,“”,$string);
这是一个类似于strip_标记的函数,只是它只删除指定的标记(具有属性):

<?php
function strip_only($str, $tags) {
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
    return $str;
}
?>

所以你会像这样使用它

<p><?php $content = get_the_content();  
                if (mb_strlen($content) > 700) {
                $content = mb_substr($content, 0, 700);
                // make sure it ends in a word by chomping at last space
                $content = mb_substr($content, 0, mb_strrpos($content, " ")).'...<br /><span class="landing_latest_articles_read_more"><a href="" title="">Read More</a></span>';
$content =  strip_only($content, '<b>');   //you want to remove <b> tag            
}
                echo $content; ?></p>

这起作用了

或者这可能有用

$string = preg_replace("/<b>|</b>/", "", $string);
$string=preg_replace(“/|/”,“”,$string);
这是一个类似于strip_标记的函数,只是它只删除指定的标记(具有属性):

<?php
function strip_only($str, $tags) {
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
    return $str;
}
?>

所以你会像这样使用它

<p><?php $content = get_the_content();  
                if (mb_strlen($content) > 700) {
                $content = mb_substr($content, 0, 700);
                // make sure it ends in a word by chomping at last space
                $content = mb_substr($content, 0, mb_strrpos($content, " ")).'...<br /><span class="landing_latest_articles_read_more"><a href="" title="">Read More</a></span>';
$content =  strip_only($content, '<b>');   //you want to remove <b> tag            
}
                echo $content; ?></p>


这起作用了

如果您只希望删除粗体标记:

$content = preg_replace('/<[\/]?b>/i', '', $content);
                            ^
$content=preg_replace('//i',''$content);
^

尽管您必须确保只有
标记使内容变为粗体,而不是字体标记。

如果您只想删除粗体标记:

$content = preg_replace('/<[\/]?b>/i', '', $content);
                            ^
$content=preg_replace('//i',''$content);
^


尽管您必须确保只有
标记使内容加粗,而不是字体标记。

内容中还有其他标记您不想删除吗?您也可以使用
修剪($content)
@JMC轻松去掉尾随空格。我说的是加粗字母,而不是空格。请阅读或误读代码中的注释。内容中是否有任何其他标记您不想删除?您也可以使用
trim($content)
@JMC轻松删除尾随空格。我指的是粗体字母,而不是空格。请阅读操作错误阅读代码中的注释。如果regexp肯定会返回错误,则必须转义分隔符。。。另外,如果可以使用
str_replace(数组(“”、“”、“”)、“$string”),为什么要使用regexp我是PHP新手。你能告诉我该代码在我已经存在的代码循环中是如何工作的吗?似乎当我把它放进去时,我从
$content=strip_only($content,')中得到了这个错误(意外的“}”)。
@Holler:希望你没有错过“;”行中的“$content=strip_only($content,”);请添加它,这样错误就不会出现。我现在已经更改了它。你也这样做。我也这么做了。现在我有了函数和您的代码,但粗体并没有被删除:(regexp肯定会返回一个错误,您必须转义分隔符…而且,如果您可以使用
str_替换(数组(“”,“,”,“”),“,”,$string),为什么要使用regexp呢)
我是PHP新手。你能告诉我该代码在我已有的代码循环中是如何工作的吗?似乎当我把它放进去时,我从
$content=strip\u only($content,,)
@Holler:希望你没有错过“;”行中的“$content=strip\u only($content,,”);添加该函数,则不会出现此错误。我现在已对其进行了更改。您也执行了相同的操作。我现在有了函数和您的代码,但粗体不会被删除:(