开发者

mysterious whitespace when putting a string togehter

开发者 https://www.devze.com 2023-04-05 20:58 出处:网络
This may seem odd but I\'m simply trying to put a url together. The first part ($first) I get from user input using strrpos() and substr() with \"/\".

This may seem odd but I'm simply trying to put a url together. The first part ($first) I get from user input using strrpos() and substr() with "/". The exact file I want to get to is fixed ($second) so all I think I need to do is this:

$first = "http://www.somedomain.de/somepath/";
$second = "thexml.xml";
$url = $first.$second;

BUT: Although I use trim() on every part there still is some whitespace between the two parts when I print $url.

When I try to navigate to $url the whitespace is replaced by a "%". The path itself is correct, when I get rid of the whitespace/ % manually in my browser's adress bar.

I also tried putting the two strings together with an array and implode() but the output stays the same. What am I doing wrong?

Update from Lisa

ok, so I printed $first开发者_Go百科 and $second separately and there are no whitespaces. it seems to be appearing when I concatenate them and exactly where the two strings are put together.

Any other ideas?


ok, so I printed $first and $second separately and there are no whitespaces. it seems to be appearing when I concatenate them and exactly where the two strings are put together.

Any other ideas?


Most likely, the code you're editing or the inputs (user, database, etc.) is not what you expect. Try trimming down (no pun intended) the code to a minimal example. For example, go from

$first = "http://www.somedomain.de/somepath/";
$second = "thexml.xml";
$url = $first.$second;
echo $url; // No space

to

$first = $_POST['url'];
$second = "thexml.xml";
$url = $first.$second;
echo $url; // If this contains a space, the input contains the offending space

step-by-step to find the mistake.


Have you tried rawurlencode? http://php.net/rawurlencode


I know this is a very old question but I just had this similar issue and was finally able to find a solution.

My solution is to use the trim function like this:

$url = trim($first).$second;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号