if (in_array(<title>, $SomeArray)) return true
I have problem that it cant detect every word in string. similar to filter or tag or category or sort of..
$title = "What iS YouR NAME?";
$english = Array( 'Name', 'Vacation' );
if(in_array(strtolower($title),$english)){
$language = 'english';
} else if(in_array(strtolower($title),$france)){
$language = 'france';
} else if(in_array(strtol开发者_StackOverflowower($title),$spanish)){
$language = 'spanish';
} else if(in_array(strtolower($title),$chinese)){
$language = 'chinese';
} else if(in_array(strtolower($title),$japanese)){
$language = 'japanese';
} else {
$language = null;
}
output is null.. =/
No real problem here... the string in $title
isn't in any of the arrays you are testing with, even in lower case.
Try testing every word in each language tab against the string instead.
$english = Array( 'Name', 'Vacation' );
$languages = array('english'=>$english,
'france'=>$france,
'spanish'=>$spanish,
'chinese'=>$chinese,
'japenese'=>$japenese);
while($line = mysql_fetch_assoc($result)) {
$title = $line['title'];
//$lower_title = strtolower($title); stristr() instead.
$language_found = false;
foreach($languages as $language_name=>$language) {
idx = 0;
while(($language_found === false) && idx < count($language)) {
$word = $language[$idx];
if(stristr($title, $word) !== false) {
$language_found = $language_name;
}
$idx++;
}
}
// got language name or false...
}
You could also breaking the string using explode
of course and testing each word in the created array for each language.
The output is null
because the string "what is your name?"
is not in any of the language arrays. Note that you should not write language names in code.
Instead, a dictionary or array of languages (in the form of dictionaries or objects) allows future extension and separates data from control logic.
There are several logic issues:
The first problem is that you are trying to see if a multi-word string is in an array of single words. That will always fail to find a match. You would need to break up $title
with explode and loop over the words.
ie) $title_words = explode(' ', strtolower($title));
foreach($title_words as $word){
//check language
//now you can use in_array and expect some matches
}
However that presents a second issue.What if a word is in multiple languages?
A third issue is in your sample, you have converted your search string to lowercase, but your array of matches has all words with an upper case first letter.
ie)
$english = Array( 'Name', 'Vacation' );
should be
$english = array( 'name', 'vacation' );
if you expect matches
精彩评论