猿问

警告:stripos()在php中至少需要2个参数

我在php中编写了一个简单的函数,并根据传递的数组索引值将参数传递给大写字母,但出现此错误


警告:stripos()至少需要2个参数


我做错了什么,任何人都可以建议我。


我是php的新手,现在才开始学习。


<?php

 function doCapital($string, $array)

 {

     $stringArray = explode(",", $string); 


     for( $i=0; $i<count($stringArray); $i++)

     {

         if(stripos($stringArray)>-1){

             $stringArray[$i] = $stringArray[$i].ucfirst();

             echo $stringArray[$i];

         }

     }


     return implode(" ",$stringArray);

 }


 echo doCapital('abcd', [1,2]);


互换的青春
浏览 241回答 2
2回答

繁华开满天机

抱歉,在重读我的最后一个答案时,我意识到它似乎非常不友好-我猛击了一个快速答案,并且没有读回来。我要说的是,出现这样的错误是最快的解决方案,请转到php手册并检查所需的参数-在这种情况下,这是一个针和一个干草堆(即要搜索的内容和要搜索的内容)。您可能会在这里发现相同的错误, $stringArray[$i] = $stringArray[$i].ucfirst();因为ucfirst要求传递一个字符串-在这里您像jQuery一样使用它,因此php认为您正在尝试连接一个字符串,它应该说ucfirst($stringArray[$i])您也不能用逗号爆炸,除非您的字符串包含它们,所以在示例中,您将收到相同的字符串,我想您的意思是使用类似 str_split我还要重申,我认为您需要使用in_array自己想要实现的目标,例如:function doCapital($string, $array){&nbsp; &nbsp; $stringArray = str_split($string);&nbsp;&nbsp; &nbsp; foreach($stringArray as $key => $value)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //see if the key exists in the array of characters to change the case for&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //and update if it does&nbsp; &nbsp; &nbsp; &nbsp; if(in_array($key,$array)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stringArray[$key] = ucfirst($value);//thinking about it I might just use strtoupper since there's only one letter anyway - I'm not sure that there's any real performance benefit either way&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return implode("",$stringArray);&nbsp;}&nbsp;echo doCapital('abcd', [1,2]); //outputs aBCd

蝴蝶不菲

stripos-查找不区分大小写的子字符串在字符串中首次出现的位置您缺少第二个参数,使用该函数的正确语法stripos是stripos&nbsp;($haystack&nbsp;,$needle);这里$haystack->您要搜索的字符串$needle&nbsp;->子字符串例如 :$findme&nbsp; &nbsp; = 'x';$mystring1 = 'xyz';$pos1 = stripos($mystring1, $findme);if ($pos1 !== false) {&nbsp; &nbsp;echo "We found '$findme' in '$mystring1' at position $pos1";}
随时随地看视频慕课网APP
我要回答