PHP:strpos()无法找到';”';


PHP: strpos() fails to find '“'

我看了另一个问题,但答案对我来说不是真的。

$text:

(string:104) “The diamond cannot be polished without friction, nor the man perfected without trials.” Chinese Proverb

然而。。。

strpos($text, '“')

===错误,经检查。

同:

strpos($text, "“")
strpos($text, "'“")
strpos($text, ''“')

想法?

EDIT:如果有区别的话,它是从文本区域获取的$_POST变量。该值正是

“The diamond cannot be polished without friction, nor the man perfected without trials.” Chinese Proverb

也许文本中的引号字符与您在代码中指定的字符不同。或者,文本可能使用与代码不同的字符编码。检查在哪个字符集中查看输出,并查看它是否与PHP脚本的编码匹配。

编辑:

function str_ord ($str) {
    $len = strlen($str);
    $my_array = array();
    for ($i = 0; $i < $len; ++$i) {
        $my_array[] = ord($str[$i]);
    }
    return implode(' ', $my_array);
}

找出str_ord()'“'和文本区域字符串的前几个字符输出的内容。如果它输出不同的数字,那么就存在字符编码问题。

您是否真的将(string:104)作为$text值的一部分?

我问这个问题是因为我用字符串进行了测试:

$text = '(string:104) “The diamond cannot be polished without friction, nor the man perfected without trials.”';

它返回第一个智能引号的数字。

然后我测试了:

$text = '“The diamond cannot be polished without friction, nor the man perfected without trials.”';

它显然返回0,这也等于false。你确定你检查的返回值是正确的吗?

例如:

$text = '“The diamond cannot be polished without friction, nor the man perfected without trials.”';
echo strpos($text, '“') !== false ? 'Yay' : 'Nay';

试试这个:

strpos($text, """");
<?php
$text = '(string:104) "The diamond cannot be polished without friction, nor the man perfected without trials." Chinese Proverb';
echo strpos($text, '"');
?>

13

我不确定你的问题是什么。它在本地似乎工作得很好,可能你定义了没有上述字符的字符串?

我不知道你的PHP出了什么问题。我在下面的测试得到了结果:

<?php
$text = "   “The diamond cannot be polished without friction, nor the man perfected without trials.” Chinese Proverb";
echo strpos($text, '“');

它产生:

3

包含表单的页面可能使用了与php脚本所期望的不同的字符集。它们需要相互一致。

尝试将这一行添加到为表单提供服务的php页面的顶部,并为接收表单post的php页面添加

header('Content-Type: text/html; charset=utf-8');

酸柠檬,

$str='"This text"';
echo strpos($str, '"');

产生0,这是错误的,因此需要明确地与错误进行比较,即

if(strpos($str, '"')!==false)
   echo "Found it.";
else
 echo "Dangit, not again! Why wont this thing work?";`

生成"找到它"。