Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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 在提取的描述中突出显示提取的关键字_Php - Fatal编程技术网

Php 在提取的描述中突出显示提取的关键字

Php 在提取的描述中突出显示提取的关键字,php,Php,好的,假设我们想要得到网站的标题、关键字和描述,所以我将使用以下函数 <?PHP function getInfo($URL) { $getInfo= get_meta_tags($URL); return $getInfo; } $URL = "http://www.my_site.com"; // URL // Applying the function $_getInfo = getInfo($URL); // Print the results. echo

好的,假设我们想要得到网站的标题、关键字和描述,所以我将使用以下函数

<?PHP

function getInfo($URL)
{
    $getInfo= get_meta_tags($URL);
    return $getInfo;
}

$URL = "http://www.my_site.com"; // URL

// Applying the function
$_getInfo = getInfo($URL);

// Print the results.

echo $_getInfo ["keywords"]."<br>"; // gives keywords
echo $_getInfo ["description"]."<br>"; // gives description

?>
说明

Advanced profitable and featured script to share
在本例中,我们在description
proficible
share
中找到了一些关键字

问题是如何突出显示仅在描述中找到的关键字

我将添加以下css

<style>
.highlight{background: #CEDAEB;}
.highlight_important{background: #F8DCB8;}
</style>
现在同时应用这两种方法(不起作用)

为什么不工作,因为它将
$\u getInfo[“关键字”]
定义为一个词
php,有利可图,共享
事实上,在这种形状的描述中找不到

那么如何使用
explode
foreach
(我猜)来应用它呢


我不知道如果我的方法看起来不好,是否还有其他方法来做谢谢

因为您的关键字是列表格式的,您需要:

foreach(explode(',', $keywords) as $keyword)

你的关键词真的像你展示的那样是逗号分隔的列表吗?如果是这样,您需要
foreach(将(',',$keywords)分解为$keywords)
哦,我的上帝。。我是多么愚蠢,我用空格爆炸,而它应该用逗号@DigitalChris。非常感谢。好的,我回答了。
<?PHP
    function hightlight($str, $keywords = '')
    {
        $keywords = preg_replace('/\s\s+/', ' ', strip_tags(trim($keywords))); // filter
        $style = 'highlight';
        $style_i = 'highlight_important';
        $var = '';
        foreach (explode(' ', $keywords) as $keyword) {
            $replacement = "<span class='".$style."'>".$keyword."</span>";
            $var .= $replacement." ";
            $str = str_ireplace($keyword, $replacement, $str);
        }
        $str = str_ireplace(rtrim($var), "<span class='".$style_i."'>".$keywords."</span>", $str);
        return $str;
    }
?>
$string = hightlight($_getInfo["description"], $_getInfo ["keywords"]);
echo $string;
foreach(explode(',', $keywords) as $keyword)