How to encode and decode / character with PHP - php

In my dynamically generated website sometimes parts of my URLs contain a / character:
Serena Williams/Venus Williams-Andrea Hlavackova/Lucie Hradecka
And, naturally, the URL returns a 404 error as the / sign is considered a folder so the URL doesn't exist.
What would be the PHP function to encode and later decode the string which contains a / character?

You can use PHP: urlencode to encode and its counterpart PHP: urldecode to decode:
urlencode($stringinput)

You can user htmlentities() whit urlencode()
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="index.php?' . htmlentities($query_string) . '">';
?>

Related

PHP prevent html entity creation at string concatenation

How can i prevent that PHP converts a recognized part of a string to an html-entity?
So e.g. lets say i have to concat parts together to an url, like:
echo '&' . 'section=' . '<br>';
$a = '&a';
$b = 'mplitude=';
echo "{$a}{$b}" . '<br>';
echo sprintf("%s%s", '&quote', '=');
the code above prints the following:
§ion=
&litude=
"e=
instead of:
&section=
&amplitude=
&quote=
how can this be prevented without throwing filters on it trying to convert the symbols back to an string again?
You need using htmlspecialchars function:
echo htmlspecialchars('&' . 'section=' . '<br>');

How to convert apostrophe or quotes in url string?

I am trying to convert the apostrophe in URL string using htmlentities() or htmlspecialchars() .... but it is not working for me...
I have following code:
<?php
$new = htmlspecialchars("<a href='http://abc.test.net/content/22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman'>Test</a>");
echo $new;
?>
but I am getting the output from $new:
<a href='http://abc.test.net/content/22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman'>Test</a>
How to convert the apostrophe and single quotes in url...
Try to use urlencode("your URL") only on the part you need (otherwise it will mess up the rest of the URL):
$new = "<a href='http://abc.test.net/content/" . urlencode("22799-mdsap-partners-with-sap’s-‘moving-experience’-initiative-in-the-uae-and-oman") . "'>Test</a>";
echo $new;

Base 64 encode - twice

I need to base 64 encode parts of a URL for an S3 URL.
I'm left with something like:
http://d111111abcdef8.cloudfront.net/image.jpg?color=red&size=medium
&Expires=1357034400
&Signature=nitfHRCrtziwO2HwPfWw~yYDhUF5EwRunQA-j19DzZrvDh6hQ73lDx~-ar3UocvvRQVw6EkC~GdpGQyyOSKQim-TxAnW7d8F5Kkai9HVx0FIu- 5jcQb0UEmatEXAMPLE3ReXySpLSMj0yCd3ZAB4UcBCAqEijkytL6f3fVYNGQI6
&Key-Pair-Id=APKA9ONS7QCOWEXAMPL
As you can see Signature and Key Pair ID are encoded.
I need to use the above URL as a param in another URL.
I have base64 encoded (as to mask the domain, makes it a liitle prettier) and then URL encoded this.
My question is, with having certain params base 64 encoded, then base 64 encoding the entire string again, upon decode, will the original params such as Signature and Key Pair ID be readable?
Simple question, simple answer: Yes.
If you are going to do this, you will need to use "&" instead of "&" in the string you are encoding. Also, base64_decoding the entire encoded string, will only decode the last encoding.
An example:
$string1 = "This is a string";
$string2 = "This is another String";
$string1 = base64_encode( $string1 );
$string2 = base64_encode( $string2 );
echo $string1 . "<br />";
echo $string2 . "<br />";
$entity = "HTTP://www.google.com/?param1=" . $string1 . "&param2=" . $string2;
$encoded_entity = base64_encode( $entity );
echo $encoded_entity . "<br />";
$decoded_entity = base64_decode( $encoded_entity );
echo $decoded_entity . "<br />";
This will output:
VGhpcyBpcyBhIHN0cmluZw==
VGhpcyBpcyBhbm90aGVyIFN0cmluZw==
SFRUUDovL3d3dy5nb29nbGUuY29tLz9wYXJhbTE9VkdocGN5QnBjeUJoSUhOMGNtbHVadz09JmFtcDtwYXJhbTI9VkdocGN5QnBjeUJoYm05MGFHVnlJRk4wY21sdVp3PT0=
HTTP://www.google.com/?param1=VGhpcyBpcyBhIHN0cmluZw==&param2=VGhpcyBpcyBhbm90aGVyIFN0cmluZw==
So as you can see, you can decode the entire string, but only the string you encoded will be decoded. Not all the levels of the encoded string. For that you will first have to decode the string, and then after that, decode the parameters.

printing a php variable as it is : with all the special characters

Ok I need to find out what is contained inside a PHP variable and I have it to do it visually, is there a function to display whatever that's contained in a string as it is?
For example :
$TEST = '&nbsp' . "\n" . ' ';
if I use echo the output will be :
while i want it to be :
&nbsp\n&nbsp
is it possible? (I hope I was clear enough)
ty
You can use json_encode with htmlspecialchars:
$TEST = ' ' . "\n" . ' ';
echo json_encode(htmlspecialchars($TEST));
Note that json_encode has third agrument in PHP 5.4.
var_dump() should do the work for you?
Example:
echo "<pre>";
var_dump($variable);
echo "</pre>";
Use <pre> to keep the format structure, makes it alot easier to read.
Resources:
http://php.net/manual/en/function.var-dump.php
http://www.w3schools.com/tags/tag_pre.asp
Try print_r, var_dump or var_export functions, you'll find them very handy for this kind of needs!
http://www.php.net/manual/en/function.htmlspecialchars.php
or
http://www.php.net/manual/en/function.htmlentities.php
$TEST = '&nbsp' . "\n" . ' ';
echo htmlspecialchars(str_replace('\n','\\n', $TEST), ENT_QUOTES);
or
$TEST = '&nbsp' . "\n" . ' ';
echo htmlentities(str_replace('\n','\\n',$TEST), ENT_QUOTES);
You may have to encode the newlines manually. If you want to encode them as actual newlines you can use nl2br. Or string replace these characters with your preference. Update: as I have added to the code per request. String replace special characters you wish to see like newlines and tabs.
assuming you want it for the debugging purposes, let me suggest to use urlencode(). I am using it to make sure I don't miss any invisible character.
The output is not that clear but it works for me.

how to define variable from mysql output

I have the following variable which returns my URL as needed. But i need to run str_replace() on it to replace a character before echoing it into my HTML code.
$url = str_replace("%3A", ":", " . nl2br( $row['url']) . ");
As it stands the " . nl2br( $row['url']) . " contains %3A instead of the colon in the URL and for some reason its rendering my links like this
http://www.mydomain.com/http%3A//url.com
I'm not really sure what your question is, but it looks like this is what you want:
$url = urldecode($row['url']);
The %3A is a URL encoded colon (:).

Categories