Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将空格替换为不间断空格?_Php - Fatal编程技术网

Php 如何将空格替换为不间断空格?

Php 如何将空格替换为不间断空格?,php,Php,我有一个解析URL的解析器 在一些解析的url中,我有一个简单的空格,如%20,但是如果我检查解析站点的真实url,我会看到这个非中断空格%C2%A0 那么,我如何将%20替换为%C2%A0 foreach($productUrls as $href) { $productWithSpace = str_replace('%20', '%C2%A0', $href['url']); $link = 'http://optnow.ru/' . $productWithSpace;

我有一个解析URL的解析器

在一些解析的url中,我有一个简单的空格,如%20,但是如果我检查解析站点的真实url,我会看到这个非中断空格%C2%A0

那么,我如何将%20替换为%C2%A0

foreach($productUrls as $href) {
   $productWithSpace = str_replace('%20', '%C2%A0', $href['url']);
   $link = 'http://optnow.ru/' . $productWithSpace;
   $product = file_get_html($link);
}
但它仍然不起作用

我的错在哪里


谢谢

我相信你的要求

urlencode将空间转换为“+”,但如果您希望它们为%20,可以使用rawurlencode来解决这个问题

$productWithSpace = rawurlencode(urldecode($href['url']));
$link = 'http://optnow.ru/' . $productWithSpace;
$product = file_get_html($link);
我会使用

urldecode($string)
它将为您自动解码任何URL。减少用户出错的机会。此外,它可以解码您可能不知道需要解码的内容

因此,您的新代码将是(这将解决您的问题):


我相信你的要求

urlencode将空间转换为“+”,但如果您希望它们为%20,可以使用rawurlencode来解决这个问题

$productWithSpace = rawurlencode(urldecode($href['url']));
$link = 'http://optnow.ru/' . $productWithSpace;
$product = file_get_html($link);
我会使用

urldecode($string)
它将为您自动解码任何URL。减少用户出错的机会。此外,它可以解码您可能不知道需要解码的内容

因此,您的新代码将是(这将解决您的问题):


$href['url']
是如何填充的
superglobals$\u GET和$\u REQUEST已被解码。
尝试如何填充
$href['url']
超级全局变量$\u GET和$\u请求已被解码。
try