我不明白为什么相同的字符串具有不同的长度。我说的是输出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"
根据您的原始帖子,$ 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中测试您的代码,您会发现其中的区别。