我试图确定用户是否发送了包含特定字符串的文本消息。
对于strpos总是返回false,我有一个奇怪的问题,除非strpos函数的指针参数被硬编码为字符串,即"Sam"。我正在通过mysqli_fetchassoc获取所需指针变量的值,并将其与从$_REQUEST['Body']检索的文本消息体进行比较。
while ($row = mysqli_fetch_assoc($queryResult))
{
if (mysqli_num_rows($queryResult) > 1)
{
//$_REQUEST['Body'] is the body of the incoming text message that Twilio will process
$messageBody= $_REQUEST['Body'];
$recName = $row['rFName'];
$recId = $row['recipientID'];
if (strpos($messageBody, $recName) !== false)
...如果我将指针参数硬编码为字符串,如下面所示,那么它工作得很好。
//This works
if (strpos($messageBody, "Sam") !== false)
...我已经对这两个比较值进行了类型检查,并确保它们是字符串。我一定是错过了一些简单的东西。
发布于 2017-03-27 04:42:11
我想通了。
斯特波斯注意空格,去数字。在我的场景中,我在测试字符串的末尾有一个空格,如下所示:Sam。(在Sam末尾添加一个空格)如果有此问题,请确保检查字符串中的任何空闲空格。
发布于 2017-03-27 04:38:09
重复检查正在与mb_detect_encoding比较的两个字符串的编码,因为strpos可以有比较UTF-8和ASCII字符串的问题。具体地说:
# Now, encoding the string "Fábio" to utf8, we get some "unexpected" outputs.
# Every letter that is no in regular ASCII table, will use 4 positions(bytes).
# The starting point remains like before.
# We cant find the characted, because the haystack string is now encoded.
var_dump(strpos(utf8_encode("Fábio"), 'á'));
#bool(false)https://stackoverflow.com/questions/43037799
复制相似问题