Space image file names

I get an array of image url over php scan image folders. Some image file names have a space. Part after a space is gone. for example: This file is in order:

http://domain.com/folder/blue-sky.png 

This file will lose part of sky.png

 http://domain.com/folder/blue sky.png 

My folder scanning code has nothing to do with checking or managing file names. How can I get the full name of blue sky.png without renaming it to blue-sky.png ?

+4
source share
3 answers

The url-encoded space is represented by the string "% 20" so you can use str_replace to replace each instance of the character "" with the character "% 20"

 echo str_replace(' ', '%20', 'http://domain.com/folder/blue sky.png'); 

displays

 http://domain.com/folder/blue%20sky.png 

Additional Information

Also, I never used it myself, but I would look at the urlencode php function, if I were you, this might contain useful information

Note: Url encode converts all characters that are not standard for the string (so you can only use urlencode in the string, which is your image name)

+15
source

You can use regex.

to try

 $file_name = "http://domain.com/folder/blue sky.png"; $file_name2 = preg_replace("/ /", "-", $file_name); echo $file_name2; //echos http://domain.com/folder/blue-sky.png 

hope this helps ...

0
source

I ran into this problem, but after experimenting with the answers above, I realized that the real problem was that I wrote an html link without inverted commas - browsers are tolerant of this, but not if there are spaces. I encoded something like a href = .. / folder / blue sky.png
instead of a href = "../folder/blue sky.png"
If the correct syntax spaces in the address do not cause problems

0
source

All Articles