我想转换像
这样的给定文字“Lorem ipsum dolor sit amet,consectetur adipiscing elit!Nam suscipit | Auctor pellentesque。“
到页面中的meta关键字。为此我需要安排文本并过滤除文本之外的所有内容(没有标点符号)。
所有都应该是小写的,并且应该省略包含3个或更少字符的单词。最终输出应如下所示:
“lorem,ipsum,dolor,amet,consectetur,adipiscing,elit,suscipit, auctor,pellentesque“
我到处寻找解决方案,但由于我是php的新手,所以对我来说几乎就像中文一样。
答案 0 :(得分: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);
编辑:从导致错误的代码中删除了一个额外的括号
答案 1 :(得分: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
答案 2 :(得分: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();
答案 3 :(得分: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);