相同字符串不同长度
Same string with different length
我不明白为什么相同的字符串有不同的长度。我说的是输出 1 和 4。
编码有问题吗?输出4,字符串是从一个网站上抓取的。
代码:
public static function findCategoryByName($category){
$value = "IT & Software";
$category_1 = urlencode($value);
$category_2 = urlencode('IT & Software');
echo '<pre>';
var_dump("1: " . $value);
echo '</pre>';
echo '<pre>';
var_dump("2: " . $category_1);
echo '</pre>';
echo '<pre>';
var_dump("3: " . $category_2);
echo '</pre>';
echo '<pre>';
var_dump("4: " . utf8_encode($category));
echo '</pre>';
echo '<pre>';
var_dump("5: " . $category);
echo '</pre>';
return (new self)->find("name=:name", "name={$category}")->fetch();
}
输出:
string(16) "1: IT & Software"
string(18) "2: IT+%26+Software"
string(18) "3: IT+%26+Software"
string(20) "4: IT & Software"
string(24) "5: IT+%26amp%3B+Software"
根据您原来的 post,$category 值为 IT & Software
,结果为:
string(16) "1: IT & Software"
string(18) "2: IT+%26+Software"
string(18) "3: IT+%26+Software"
string(20) "4: IT & Software"
string(24) "5: IT+%26amp%3B+Software"
您似乎是在浏览器中执行的,请尝试在 CLI 中测试您的代码,您会看到不同之处。
我不明白为什么相同的字符串有不同的长度。我说的是输出 1 和 4。 编码有问题吗?输出4,字符串是从一个网站上抓取的。
代码:
public static function findCategoryByName($category){
$value = "IT & Software";
$category_1 = urlencode($value);
$category_2 = urlencode('IT & Software');
echo '<pre>';
var_dump("1: " . $value);
echo '</pre>';
echo '<pre>';
var_dump("2: " . $category_1);
echo '</pre>';
echo '<pre>';
var_dump("3: " . $category_2);
echo '</pre>';
echo '<pre>';
var_dump("4: " . utf8_encode($category));
echo '</pre>';
echo '<pre>';
var_dump("5: " . $category);
echo '</pre>';
return (new self)->find("name=:name", "name={$category}")->fetch();
}
输出:
string(16) "1: IT & Software"
string(18) "2: IT+%26+Software"
string(18) "3: IT+%26+Software"
string(20) "4: IT & Software"
string(24) "5: IT+%26amp%3B+Software"
根据您原来的 post,$category 值为 IT & Software
,结果为:
string(16) "1: IT & Software"
string(18) "2: IT+%26+Software"
string(18) "3: IT+%26+Software"
string(20) "4: IT & Software"
string(24) "5: IT+%26amp%3B+Software"
您似乎是在浏览器中执行的,请尝试在 CLI 中测试您的代码,您会看到不同之处。