array matching and display in php
i am using PHP scripts to implement this...
$keyword=array('local news','art','local','world','tech','entertainment','news','tech','top stories','in the news','front page','bbc news','week in a glance','week in pictures','top stories');
//$keyword has predefined array of strings
$all_meta_tags=get_meta_tags("http://abcnews.go.com/");
$array=$all_meta_tags['keywords'];//store 'keyword' attribute values in $keyword_meta
Now i have to match contents of $array with $keyword.....the results should give me matched items of $array which are present in $keyword
any help plz...?
can array matching/intersection be done case insensitively?? i me开发者_开发问答an if $keyword=array('local news'); $array = 'Local News, International News'; var_dump(array_intersect(preg_split('/,\s*/', $array), $keyword));
then it won't match 'Local News'...can you tel me hw to do it if it is possible??
$inBoth = array_intersect(preg_split('/,\s*/', $array), $keyword);
CodePad.
get_meta_tags()
just returns the keywords as a string, so we need to split it into an array. We take into account people adding spaces, newlines or tabs after the ,
.
You could also skip the regex, and explode on ,
and then use array_map('trim', $array)
.
Without doing this, you run the risk of "art"
and " art"
not matching.
Update
can array matching be done case insensitively?
If you don't mind the resulting arrays being lowercase, you could use array_map('strtolower', $array)
on both arrays before using array_intersect()
.
Otherwise, this will do it...
$metaKeywords = preg_split('/,\s*/', $array);
$matches = array();
foreach($keyword as $keyword) {
foreach($metaKeywords as $value) {
if (strtolower($value) == strtolower($keyword)) {
$matches[] = $keyword;
}
}
}
$matches
will have keywords in both arrays case insensitively.
If you have multibyte strings, use mb_strtolower()
or equivalent.
You need to use array_intersect()
http://php.net/manual/en/function.array-intersect.php
精彩评论