PHP将文本转换为元关键字
我想转换一个给定的文本,就像 “Lorem ipsum door sit amet,献祭精英!Nam suscipit |拍卖人佩伦茨克。” 在页面中添加元关键字。为此,我需要安排文本并过滤除文本以外的所有内容(无标点符号) 所有文字均应为小写,且包含3个或更少字符的文字应省略。最终输出应如下所示: “lorem,ipsum,dolor,amet,Concertetur,Adipising,Elite,suscipit, 佩伦茨克拍卖行“ 我到处都在寻找解决方案,但由于我是php新手,它对我来说几乎就像中文一样。希望这有帮助PHP将文本转换为元关键字,php,string,text,replace,Php,String,Text,Replace,我想转换一个给定的文本,就像 “Lorem ipsum door sit amet,献祭精英!Nam suscipit |拍卖人佩伦茨克。” 在页面中添加元关键字。为此,我需要安排文本并过滤除文本以外的所有内容(无标点符号) 所有文字均应为小写,且包含3个或更少字符的文字应省略。最终输出应如下所示: “lorem,ipsum,dolor,amet,Concertetur,Adipising,Elite,suscipit, 佩伦茨克拍卖行“ 我到处都在寻找解决方案,但由于我是php新手,它对我来说
// 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);
编辑:从导致错误的代码中删除了一个额外的括号
$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);
使用的函数:
,请尝试以下方法:
$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();
$text=“Lorem ipsum door sit amet,Concetetur adipiscing elit!Nam suscipit | Auctor pellentesque.”;
preg_match_all('/\w{4,}/i',$text,$matches);
$meta=join(“,”,$matches[0]);
印刷品(“”);
印刷(元);模具();
那么您自己做了哪些尝试?str_word_count()可能是一个有用的起点,它可以将数组_walk()转换为小写,并使用数组_filter()删除所有长度小于等于3个字符的单词。您错过了内爆()中的参数顺序。
。非常感谢。。成功了。对不起,如果我的问题听起来很愚蠢,但我确实从你们在这里写的东西中学到了很多。。
$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();