简体   繁体   中英

How to replace space to non breaking space?

I have parser which parsing urls.

In some parsed urls I have a simple space like %20 , but if I check the real url of parsed site I see this non-breaking space %C2%A0 .

So, how I can replace %20 to %C2%A0 ?

foreach($productUrls as $href) {
   $productWithSpace = str_replace('%20', '%C2%A0', $href['url']);
   $link = 'http://optnow.ru/' . $productWithSpace;
   $product = file_get_html($link);
}

But It still not working.

Where is my mistake?

Thanks

What I believe you are asking for

urlencode transforms spaces into '+'s, but if you want them to be %20, you can use rawurlencode to get around that.

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

I would use

urldecode($string)

It will decode any urls automatically for you. Less chance for user mistake. Additionally, it decodes things you might not know needs to be decoded.

So, your new code would be (and this should fix your problem):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM