Php 检查字符串数组中的字符串是否在字符串中可用
我知道php中的所有字符串函数(preg_match、strpos…)都允许我在字符串中查找字符串。我的目的是在如下字符串中查找字符串数组中的字符串:Php 检查字符串数组中的字符串是否在字符串中可用,php,Php,我知道php中的所有字符串函数(preg_match、strpos…)都允许我在字符串中查找字符串。我的目的是在如下字符串中查找字符串数组中的字符串: $pattern = array("jpg", "bmp"); $subject = $bild->dateiname; <pre> <?php $pattern = array("jpg", "bmp"); $yourString ="bmp dd ddd
$pattern = array("jpg", "bmp");
$subject = $bild->dateiname;
<pre>
<?php
$pattern = array("jpg", "bmp");
$yourString ="bmp dd ddd ddd";
$string = (explode(" ",$yourString));
echo findeStirng($pattern,$string);
function findeStirng($pattern,$string){
foreach ($string as $val){
foreach ($pattern as $valpat){
$find="";
if ($val==$valpat){
$find ="finde string";
return $find;
break;
}
}
}
}
?>
</pre>
任何想法,如何使用php函数实现我的意图?在数组中查找字符串
如果您只想查看主题字符串中是否有任何一个数组字符串,请尝试替换它们并检查是否有替换:
if(str_replace($pattern, '', $subject, $count) && $count) {
echo 'something was found';
}
或:
要捕获匹配项,请使用preg_match
或捕获所有匹配项:
然后检查
$matches
您看过手册了吗?答案就在那里。使用谷歌也一样。你需要知道找到了哪一个,或者是否找到了任何一个吗?。。。或者如果全部找到了?if(preg_match('/\.('.intlode('.$patern.))/,$subject))回显“是”代码>
if(str_replace($pattern, '', $subject, $count) && $count) {
echo 'something was found';
}
preg_match_all('/(' . implode('|', $pattern) . ')/', $subject, $matches);