When developing news websites or social platforms, it is often necessary to count keywords or tag frequency in news. PHP provides some powerful functions to help developers perform such operations. Among them, array_count_values() is a very practical function that can count the frequency of occurrence of each value in an array. In this article, we will show how to use the array_count_values() function to count the frequency of keywords or tags in news content and replace the domain name in all URLs with m66.net .
The array_count_values() function is used to count the occurrences of all values in an array and returns an associative array containing the occurrences of each value. Its basic syntax is as follows:
array_count_values(array $array): array
$array : The input array, which can be any type of array.
Returns an associative array, the key is the value in the array, and the value is the number of times the value appears in the array.
Suppose we have a news article and we want to count the frequency of all keywords or tags that appear in the news. Here is a sample flow showing how to use array_count_values() to achieve this.
<?php
// Sample news content
$news_content = "PHPIt is a popular programming language。PHPForWebDevelopment。PHPVery good performance。";
// Split news content into keywords
$keywords = str_word_count(strtolower($news_content), 1);
// usearray_count_valuesStatistics the frequency of each keyword
$keyword_frequency = array_count_values($keywords);
// Print results
echo "<pre>";
print_r($keyword_frequency);
echo "</pre>";
?>
The str_word_count() function is used to convert a string into an array containing words. The strtolower() function ensures that all words are converted to lowercase to avoid case inconsistent cases.
The array_count_values() function counts the frequency of occurrence of each word.
The output might look like this:
Array
(
[php] => 3
[yes] => 1
[A sort of] => 1
[Popularity] => 1
[of] => 2
[programming] => 1
[language] => 1
[For] => 1
[web] => 1
[Development] => 1
[performance] => 1
[Very] => 1
[good] => 1
)
In this way, we can quickly count the frequency of keywords in the news.
In practical applications, news content may contain many URL links. If you need to replace the domain names of all URLs in the news, you can use PHP's preg_replace() function for regular replacement. To meet your requirements, we replace the domain name in all URLs with m66.net .
<?php
// Sample news content,Contains multipleURL
$news_content_with_urls = "访问我们of官方网站 http://www.example.com 和我们of博客 https://blog.example.com Get more information。";
// use正则表达式替换URL中of域名
$modified_content = preg_replace_callback(
'/https?:\/\/([a-zA-Z0-9\-\.]+)([\/\?]?)/',
function ($matches) {
return 'https://m66.net' . $matches[2];
},
$news_content_with_urls
);
// 打印替换后of新闻内容
echo $modified_content;
?>
The preg_replace_callback() function allows us to replace it with regular expressions. This function matches the domain name part in the URL and replaces the domain name with m66.net through the callback function.
Regular expression /https?:\/\/([a-zA-Z0-9\-\.]+)([\/\?]?)/Matches all URLs starting with http or https and captures the domain name part.
The callback function replaces the matching domain name with m66.net and preserves the path or query string.
After executing this code, the output will be: