php - substr 与字符串中的破折号一起工作很奇怪

标签 php string

我有以下三种情况,我希望显示相同的输出,但它对 $str1 做了一些奇怪的事情:

<?php
$str1 = "— lorem lorem Alice lorem lorem lorem loremlorem";
//       | < Why would a dash make a difference in the the found index?
$str2 = "a lorem lorem Alice lorem lorem lorem loremlorem";
$str3 = "  lorem lorem Alice lorem lorem lorem loremlorem";


// The found index is always the same
$foundIndex = mb_stripos($str1, "Alice"); 
var_dump(substr($str1, $foundIndex - 6, 24), $foundIndex);

$foundIndex = mb_stripos($str2, "Alice");
var_dump(substr($str2, $foundIndex - 6, 24), $foundIndex);

$foundIndex = mb_stripos($str3, "Alice");
var_dump(substr($str3, $foundIndex - 6, 24), $foundIndex);

输出:

string(24) "m lorem Alice lorem lore"     << Why is this swapped to the right one char?
int(14)
string(24) "lorem Alice lorem lorem "
int(14)
string(24) "lorem Alice lorem lorem "
int(14)

你可以测试一下here .

我使用操作 mb_stripossubstr 来搜索字符串,这就是 PoC。

这是为什么?如何修复包含特殊字符的字符串的行为?


经过快速检查,我认为 占用了三个字节,而 substr 按字节工作,而不是按字符工作。 strlen("—") 也是 3...

如何按字符而不是按字节对字符串进行切片?按字节切片对我来说实际上不起作用。并且所有特殊字符都应该正确处理。如果我没记错的话,表情符号也有不同的尺寸!

最佳答案

您使用的破折号是多字节字符。要执行多字节安全 substr() 操作,您需要使用 mb_substr()

<?php
$str1 = "— lorem lorem Alice lorem lorem lorem loremlorem";
//       | < Why would a dash make a difference in the the found index?
$str2 = "a lorem lorem Alice lorem lorem lorem loremlorem";
$str3 = "  lorem lorem Alice lorem lorem lorem loremlorem";


// The found index is always the same
$foundIndex = mb_stripos($str1, "Alice"); 
var_dump(mb_substr($str1, $foundIndex - 6, 24), $foundIndex);

$foundIndex = mb_stripos($str2, "Alice");
var_dump(mb_substr($str2, $foundIndex - 6, 24), $foundIndex);

$foundIndex = mb_stripos($str3, "Alice");
var_dump(mb_substr($str3, $foundIndex - 6, 24), $foundIndex);

关于php - substr 与字符串中的破折号一起工作很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68740589/

相关文章:

php - 在产品类别的末尾对 opencart 特价商品进行排序

.net - 如何通过反射检索字符串并将其升序连接

vb.net - 在 Visual Basic 中何时使用 CDbl 和 CStr 函数

C、topper段错误

php - 无法通过 php 脚本进行查询

php - 如何在 SocialEngine 中编辑移动 CSS?

php - 我如何发送包含在表格中的多个复选框

java - Dice Class,Cho-Han Bakuchi 的游戏。Java(二元运算符的错误操作数类型 '+')

string - shell bash,你能检查一个函数是否存在吗

php - 在 Codeigniter、MysQL 中使用嵌套查询和 JOIN