php:如何检查字符串是否包含任何列出的关键字?
i have an array of strings.
I have an array of keywords.
i loop through each string, and need to store them to mysql database if it contains any of the keywords.
currently i am using multiple stristr(), which is getting difficult.
is it possible to do something like stristr($string, array("ship","fukc","blah"));
?
我有一个字符串数组。 p>
我有一组关键字 。 p>
我循环遍历每个字符串,如果它包含任何关键字,则需要将它们存储到mysql数据库。 p>
目前我正在使用多个字符串 stristr(),这很难。 p>
是否可以执行类似 stristr($ string,array(“ship”,“fukc”,“blah”))的操作) ; code>? p>
div>
I would advice you to use regular expresion for that
snipet:
preg_match_all('|(keyword1|keyword2|keyword3)|', $text, $matches);
var_dump($matches);
see the documentation of preg_match_all for reference
try using in_array()
for ($i = 0 ; $i < count($arrayString); $i++){
for ($j = 0 ; $j < count($arrayKeyWord); $j++){
if (in_array($arrayString[$i],$arrayKeyWord[$j]) == true){
/* mysql code */
}
}
}
$to_be_saved = array();
foreach($strings as $string) {
foreach($keywords as $keyword) {
if(stripos($string, $keyword) !== FALSE){
array_push($to_be_saved, $keyword);
}
}
}
/*save $to_be_saved to DB*/
foreach ( $strings as $string ) {
foreach ( $keywords as $keyword ) {
if ( strpos( $string, $keyword ) !== FALSE ) {
// insert into database
}
}
}
if(in_array($string, array("ship","fukc","blah")))
{
}
Documentation: http://php.net/manual/en/function.in-array.php