我有很多贴子都有多个标签,例如:AMOS LEE, FOO FIGHTERS,TAYLOR SWIFT
帖子标题中只有一个标签,例如:“The Man Who Wants You” – Amos Lee
如何扫描\\u title(),遍历标记并将匹配的标记存储在变量中?
我试过了
<?php
$id = get_the_title();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$tag = $tag->name . \'<br>\';
}
}
if (stripos($id,$tag) !== false) {
echo \'true<br>\';
}else{
echo \'false\';
}
?>
最合适的回答,由SO网友:James Barrett 整理而成
把这个放在你的循环中:
<?php
$title = get_the_title();
$posttags = get_the_tags();
if( $title && $posttags ) {
$result = find_tag_in_title( $title, $posttags );
var_dump( $result ); // dump result
}
?>
把这个放在你的函数中。php文件:
<?php
function find_tag_in_title( $title, $posttags ) {
foreach( $posttags as $tag ){
if( strpos( strtolower($title), strtolower($tag->name) ) !== false ) return $tag->name;
}
return false;
}
?>
SO网友:Domain
代码中的小更改:
//$id = get_the_title();
$title = \'"The Man Who Wants You" – Amos Lee\';
//$posttags = get_the_tags();
$posttags = array(\'AMOS LEE\', \'FOO FIGHTERS\', \'TAYLOR SWIFT\');
$matched_tags = array();
if ($posttags) {
foreach ($posttags as $tag) {
if (stripos($title, $tag) !== false) {
array_push($matched_tags, $tag);
}
}
}
print_r($matched_tags);
输出:匹配的标签
Array
(
[0] => AMOS LEE
)