PHP: convert spaces in string into %20? - php

How can I convert spaces in string into %20?
Here is my attempt:
$str = "What happens here?";
echo urlencode($str);
The output is "What+happens+here%3F", so the spaces are not represented as %20.
What am I doing wrong?

Use the rawurlencode function instead.

The plus sign is the historic encoding for a space character in URL parameters, as documented in the help for the urlencode() function.
That same page contains the answer you need - use rawurlencode() instead to get RFC 3986 compatible encoding.

I believe that, if you need to use the %20 variant, you could perhaps use rawurlencode().

Related

Convert french accent to specific encoding in PHP

In php, what function can I use to convert the text 'pétition' to 'p%E9tition'.
I have tried with uft8_encode and uft8_decode with no success.
%E9 is an URL encoded escape character. You can achieve this by urlecode($string).
If you want HTML escaping, you can either use htmlentities($string) (more encoding) or htmlspecialchars($string) (less encoding).
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.htmlentities.php
http://php.net/manual/en/function.htmlspecialchars.php
When dealing with UTF-8 strings, you will need to decode the string (ie. with utf8_decode) before encoding with urlencode to be used in a query part of a URL.
print_r( urlencode(utf8_decode('pétition')) );
// p%E9tition
You can try to have a look at htmlentities.
This link can help

PHP convert characters applicable for title tag

In my page I convert lower to uppercase string and output 'em in the title tag. First I had the issue that   is not accepted, so I had to preserve entities.
So I converted them to unicode, then uppercase and then back to htmlentities:
echo htmlentities(strtoupper(html_entity_decode(ob_get_clean())));
Now I have the problem that I recognized related to a "right single quote". I'm getting this character as ’ in the title.
It seems that either of the two functions I'm using does not convert them correctly. Is there any better function that I can use or is there something especially for the title tag?
Edit: Here is a var_dump of the original data which I don't have influence to:
string(74) "Example example example » John Doe- Who’s That? "
Edit II: This is what my code above results in:
This would happen, if I would just use strtoupper:
Your problem is that strtoupper will destroy your UTF-8 entity-decoded input because it is not multibyte aware. In this instance, ’ decodes to the hex-encoded UTF-8 sequence e2 80 99. But in strtoupper's single-byte world, the character with code \xe2 is â, which is converted to  (\xc2) -- which makes your text an invalid UTF-8 sequence.
Simply use mb_strtoupper instead.
It's ugly, but it might work for you (although I would certainly suggest Jon's solution):
After your strtoupper(), you can replace all uppercased HTMLentities this way:
$entity_table = get_html_translation_table(HTML_ENTITIES);
$entity_table_uc = array_map('strtoupper', $entity_table);
$string = str_replace($entity_table_uc, $entity_table, $string);
This should remove the need for htmlentities() / html_entity_decode().

Removing the "%" encoding in URL string

I am attempting to open a page with window.open and it's not working. The path shown is like xyz/a%20b%20c%20.pdf, but it is supposed to be xyz/abc.pdf. If I remove the % and 20 manually, it works, how can I remove these characters using PHP?
Use urldecode:
(PHP 4, PHP 5)
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
Example
echo urldecode('xyz/a%20b%20c%20.pdf');
This is known as URL Encoding. You need to decode the string. If you are using jQuery you should check out the URL Encode plug in.
You need to urldecode (as stated above).
However, you say that you can remove the %20 and it will work. I would say you need them, they decode to spaces. Check it out using this online url decoder:
http://www.convertstring.com/EncodeDecode/UrlDecode
it decodes to:
xyz/a b c .pdf
not
xyz/abc.pdf

PHP URL encoding method

How can I have PHP display this URL correctly?
Is there a working encoding method I can use that converts all of
253A%252F%252F
to
://
in
https%253A%252F%252Fvideos-private.s3.amazonaws.com%252Flesson05.flv
?
You should use double urldecode, i.e.,
$beuty_url = urldecode(urldecode("https%253A%252F%252Fbassrxprivate.s3.amazonaws.com%252Flesson05.flv"));
echo $beuty_url;
urldecode ;)
The urldecode function decodes any %## encoding in the given string.
In your "output" string you have %253A and so on.
Remove those %25 and everything will be fine.

How to convert complex filename into HTML link?

I have a directory with PDF files that I need to create an index for. It is a PHP page with a list of links:
filename
The filenames can be complicated:
LVD 2-1133 - Ändring av dumpningslina (1984-11-20).pdf
What is the correct way to link to this file on a Linux/Apache server?
Is there a PHP function to do this conversion?
You can use rawurlencode() to convert a string according to the RFC 1738 spec.
This function replaces all non-alphanumeric characters by their associated code.
The difference with urlencode() is that spaces are encoded as plus signs.
You'll probably want to use the last one.
This technique is called Percent or URL encoding. See Wikipedia for more details.
The urlencode() function will convert spaces into plus signs (+), so it won't work. The rawurlencode does the trick. Thanks.
Be sure to convert each part of the path separately, otherwise path/file will be converted into path%2Ffile. (which was what I missed)
URL encoding. I think it's urlencode() in PHP.
urlencode() should probably do what you want.
Edit: urlencode() works fine on swedish characters.
<?php
echo urlencode("åäö");
?>
converts to:
%E5%E4%F6
rawurlencode will encode "exotic" characters in a URL.

Categories