0

我想转换给定的文本,例如

“Lorem ipsum dolor sit amet,consectetur adipiscing elit!Nam suscipit | Auctor pellentesque。”

到页面中的元关键字。为此,我需要安排文本并过滤除文本之外的所有内容(无标点符号)。

全部应为小写,包含 3 个或更少字符的单词应省略。最终输出应如下所示:

“lorem、ipsum、dolor、amet、consectetur、adipiscing、elit、suscipit、auctor、pellentesque”

我到处寻找解决方案,但由于我是 php 新手,所以对我来说几乎就像中文一样。

4

4 回答 4

4

希望这可以帮助!

// Set the phrase into an array
$keywords = "Lorem ipsum dolor sit amet, consectetur adipiscing elit! Nam suscipit | Auctor pellentesque"; 

// Remove all special characters to only leave alphanumeric characters (and whitespace)
$keywords = preg_replace('/[^A-Za-z0-9\s]/', '', $keywords);

// Explode the phrase into an array, splitting by whitespace
$keywords = explode(' ', $keywords);


// Create an empty array to store keywords
$end = array();

// Loop through each keyword
foreach($keywords as $keyword){

   // Check that the keyword is greater than 3 characters long
   // If it is, add it to the $end array
   if(strlen($keyword)>3){ $end[] = strtolower($keyword); }
}

// Implode the $end array into a comma seperated list
echo implode(', ', $end);

编辑:从导致错误的代码中删除了一个额外的括号

于 2013-05-17T12:02:02.810 回答
2
function getMetaString($string) {
    preg_match_all("/[a-z0-9\-]{4,}/i", $string, $output_array);

    if(is_array($output_array) && count($output_array[0])) {
        return strtolower(implode(',', $output_array[0]));
    } else {
        return '';
    }
}

$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit! Nam suscipit | Auctor pellentesque.";

echo getMetaString($string); // Output: lorem,ipsum,dolor,amet,consectetur,adipiscing,elit,suscipit,auctor,pellentesque

使用的函数: implode , preg_match_all

于 2013-05-17T12:03:50.147 回答
1

尝试这个:

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit! Nam suscipit | Auctor pellentesque.";
        preg_match_all('/\w{4,}/i', $text, $matches);
        $meta = join(',', $matches[0]);
        print_r('<pre>');
        print_r($meta);die();
于 2013-05-17T12:07:32.837 回答
0
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit! Nam suscipit | Auctor pellentesque.';
$string = strtolower($string);
$meta   = array();
$words  = preg_split('/[^a-z]+/', $string);
foreach ($words as $word) {
    if (strlen($word) > 3) {
       $meta[] = $word;
    }
}

echo implode(', ', $meta);
于 2013-05-17T12:02:10.613 回答