This question already has answers here:
PHP URL Encoding / Decoding
(4 answers)
Closed 8 years ago.
I can print a url with the following:
<?php print $base_url . $node_url ?>
What is the standard PHP way of converting special characters?
So instead of: http://time.com/3525666/ebola-psychology-fear-symptoms/
I need http%3A%2F%2Ftime.com%2F3525666%2Febola-psychology-fear-symptoms%2F
You would use urlencode for that sort of escaping.
Other escaping functions exist for other purposes, like htmlspecialchars for making text output safely for HTML display.
use his function in php , it is built in function to encode in url format
urlencode();
Just to add, htmlspecialchars, as mentioned above in the comment can take care of few html entities, not all of them.
Use htmlentities() instead:
$query_string = 'foo=' .urlencode($foo) . '&bar=' . urlencode($bar);echo '';
Related
This question already has answers here:
Is it possible to preserve plus signs in PHP $_GET vars without encoding?
(7 answers)
Closed 9 years ago.
I am retrieving a GET variable from URL which contains "+" in between the string.
But PHP is storing "space" instead of "+"
PFB output :
in URL : key=7VR47WOtmD+acS0
php echo : echo rawurlencode$_GET['key']) ;
//returns
7VR47WOtmD acS0
Could you please tell me how to get + symbol in the php output ?
Note: do not suggest find and replace option
You need to encode reserved characters ($ & + / : ; ? # etc) if you want to use them in an URL. You need to use %2B instead of +. If you're using PHP to generate the URL, then you can use the function urlencode() to automatically encode all characters that require encoding.
Yes, that's how URLs work. The character + in URLs must, because of the URL specification, be interpreted as a space, as plain spaces are not allowed in URLs. If you want to send a true + character (or a space for that matter) you must escape it using something like encodeURIComponent("+") (in the browser) or urlencode("+") in PHP, before sending it.
This question already has answers here:
htmlentities() vs. htmlspecialchars()
(12 answers)
Closed 8 years ago.
I have read their documentation, but I still don't get when to use each of them and their difference.
Let's consider the situation of having a general string in a variable and needing to echo it inside HTML code. If it has any HTML markup in it, I want it converted to HTML code (< replaced by <, & replaced by &. If it has UTF special chars that aren't available in HTML code, it's replaced by HTML number (• replaced by •).
What's the best function for that?
A harder need: unprintable chars, like \n, char(10), char(13), etc, be replaced by their number code, in the case the string is printed inside <pre> or any special textarea so that the string be dumped.
htmlentities is a workaround for not having set the character type of the document properly. htmlspecialchars is the correct function to use for merely writing text into an HTML document.
As to your second question, I think you're looking for addcslashes.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to decode numeric HTML entities in PHP
How does one remove/replace ” characters from a string?
I've tried html_entity_decode but it don't seem to work. There are other similar characters in the string that don't seem to be converted or removed.
Any ideas?
The issue is that html_entity_decode() doesn't translate numeric entities.
I added an answer to the suggested duplicate How to decode numeric HTML entities in PHP
str_replace(array('”', '”'), '', $thestring);
Here is a working example: http://codepad.org/gXrZcxaF
Does this do what you're looking for:
http://shiflett.org/blog/2005/oct/convert-smart-quotes-with-php Converting smart quotes and other characters with PHP so they display correctly
http://ca2.php.net/manual/en/function.htmlentities.php#84612 Dealing with Numeric entities in PHP (comment on the manual)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php: using DomDocument whenever I try to write UTF-8 it writes the hexadecimal notation of it.
Here is a snippet of code:
echo "<char>".$row{'char'}."</char>";
row{'char'} is pulling back the german character ö from the database. In PHP, how can I convert that to the correct encoding to be used properly in XML?
Is there a PHP function that can convert everything as needed to the correct format for XML? Or do I need to do it character by character, like so?
echo "<format>".str_replace("&", "&", $row{'format'})."</format>";
Thanks for the help!
Without knowing what encoding you have in your database and what encoding you want in your XML output it's hard to be specific, but the iconv function could be useful to do the conversion.
Also. you should really consider using an XML DOM instead of outputting xml-as-plaintext with echo. Check out for example Reading and writing the XML DOM with PHP
. If you don't, you will most likely end up with other strange problems with your xml output down the road.
Trust me, I've been there. :-)
Passing the data pulled from the database through htmlentities() should do that. It changes "ö" to "ö".
echo "<char>".htmlentities($row{'char'})."</char>";
The PHP Manual
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 months ago.
We've got some fraction information stored in the database, e.g. ¾ ½
Short of doing a search and replace, are there any inbuilt PHP functions that will automatically convert these to proper html entities?
You can use the htmlentities() function. This will replace all special characters with their HTML equivalent. It should do the job.
htmlentities.
But you probably don't need to. Serve your page in an encoding that includes them (UTF-8, ISO-8859-1) and you can include those as literal, unescaped characters.
The answer is already given: use htmlentities(). In addition, the use of UTF-8 has been suggested, which of course is a really good idea. However, if you're planning on using htmlentities() on UTF-8 strings, use the following code (or you'll get weirdly encoded characters):
htmlentities($str, ENT_COMPAT, 'UTF-8')
As you can imagine, it sucks having to add the second and third argument all the time. For most projects I need htmlentities() in, I end up writing the a shortcut function, i.e.:
function he($str) { // shortcut function for htmlentities() with UTF-8 settings
return htmlentities($str, ENT_COMPAT, 'UTF-8');
}
try htmlentities()