如何解决此问题:查找单词的文字,PHP
编写查找文本的字一个PHP程序。 后缀通过管道与文本分开。 例如:后缀| SOME_TEXT;
input:text | lorem ips llfaa Loremipsumtext。 输出:Loremipsumtext
我的代码是这样的,但逻辑也许是错误的:
$mystring = fgets(STDIN);
$find = explode('|', $mystring);
$pos = strpos($find, $mystring);
if ($pos === false) {
echo "The string '$find' was not found in the string '$mystring'.";
}
else {
echo "The string '$find' was found in the string '$mystring',";
echo " and exists at position $pos.";
}
你的代码$ find是一个数组,strpos期望一个字符串 – rtfm
find是一个数组。你不能使用数组和echo。你可以使用print_r或var_dump。另一种方法是foreach循环。 –
您应该搜索'$ find [1]',它包含管道后面的输入部分。 – Barmar