提问人:mebird 提问时间:7/4/2020 更新时间:7/4/2020 访问量:102
查找 “ 的索引在字符串中
Finding the index of " in a string
问:
我一直有一个(希望是愚蠢的)问题,我想知道是否有人可以提供帮助。
我正在尝试确定字符串是否以 开头,即:"
"-------- Original Message
我尝试了一切--, ,但是,无论如何,我总是发现这是错误的——而不是 0。strpos($str, '"') === 0
strpos(html_entity_decode($str), '"') === 0
strpos(***, '"')
这里有更多的上下文(从 Web 表单解析 csv 行,试图找到引用消息的开头),但我空手而归。
我正在使用 php 7。来自 JS/TS 背景,所以其中一些细微差别可能只是在我的脑海中浮现。
有没有人对可能发生的事情有任何直觉?如果需要,我可以提供代码/更多上下文。昨晚试着盯着这个看了几个小时,但没有骰子。
答:
1赞
Ruperto
7/4/2020
#1
也许这会有所帮助......函数htmlspecialchars_decode转换 ”加到引号中
<?php
$str = ""--------";
echo "Position is: ";
echo strpos(htmlspecialchars_decode($str), '"');
echo "\n";
echo "Is quotation mark at the beginning?";
echo strpos(htmlspecialchars_decode($str), '"')==0;
echo "\n";
?>
输出:
Position is: 0
Is quotation mark at the beginning? 1
评论
0赞
mebird
7/4/2020
伙计,我对 php 知识的缺乏正在闪耀!在我的代码中使用它设法解决了所有问题。上述内容的输入是通过 POST 请求进行的,因此一定发生了一些古怪的事情。
下一个:引用单词并用逗号分隔
评论
strpos($str, '"') === 0
mb_strpos($str, '"') === 0