Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我尝试获取包含一些特殊符号的字符串的子字符串,它给了我一个空字符串。代码:
$iii = "<img>"; echo substr($iii, 0, 2);
当我删除“<”时,它工作得很好。任何建议为什么会发生这种情况以及如何解决?
谢谢。
我认为你输出到浏览器,浏览器认为它是 html 标签。使用 htmlspecialchars() 转义 < 和 > 字符。
echo htmlentities(substr($iii, 0, 2));
$iii = "<img>"; echo htmlspecialchars(substr($iii, 0, 2));
试试这个...