如何将空间替换为非破碎空间?

时间:2017-03-12 15:29:01

标签: php

我有解析网址的解析器。

在一些已解析的网址中,我有一个简单的空间,例如%20 ,但是如果我检查解析网站的真实网址,我会看到这个不间断的空间%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);
}

但它仍无效。

我的错误在哪里?

由于

1 个答案:

答案 0 :(得分:0)

我相信您的要求

urlencode将空格转换为' +' s,但是如果你希望它们是%20,你可以使用rawurlencode来解决这个问题。

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

我会使用

urldecode($string)

它将自动为您解码任何网址。用户错误的机会减少。此外,它还可以解码您可能不知道需要解码的内容。

所以,你的新代码将是(这应该可以解决你的问题):

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