Converting fractions to html entities [duplicate] - php

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()

Related

How to change encoding of a web page retrieved by Simple HTML DOM?

I am trying to read contents of a web page
$html = file_get_html('http://www.example.com/somepage.aspx');
Since the page's encoding is Windows-1254, and I work on a page encoded as UTF-8, I cannot replace some words which have language-specific characters.
For Example:
If I try to
$str2 = str_replace('TÜRKÇE', 'TURKCE', $str);
it does not replace.
I have tried htmlentities() function, It worked but deleted some words which contains special characters.
Work in utf-8 only. If you have some data in other encodings, convert it. If you does not know the encoding, try to define it. If you cannot, use users. Then use mb_* functions only for all string operations, It is important! some functions is not present in native php, but search its hand-make realizations on php.net/.. in comments.
After getting strings I have used iconv('Windows-1254', 'utf-8', $str) function (thanks to #pguardiario). This solved my problem.

Convert special characters from PHP variables? [duplicate]

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 '';

htmlspecialchars() x htmlentities() [duplicate]

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.

PHP: utf-8 encode, htmlentities giving weird results

I'm trying to get data from a POST form. When the user inputs "habláis", it shows up in view source as just "habláis". I want to convert this to "habláis" for purposes of string comparison, but both utf8_encode() and htmlentities() are outputting habláis, and htmlspecialchars() does nothing. I would use str_replace but it won't recognize the á when it searches the string.
I'm using a charset of utf-8 consistently across pages. Any idea what's going on?
You are probably not specifying UTF-8 as the character set for the htmlentities() operation.
I'm not sure if this is your problem, but are you calling htmlentities with the UTF-8 parameter? I ask because that's not its default:
Like htmlspecialchars(), it takes an
optional third argument charset which
defines character set used in
conversion. Presently, the ISO-8859-1
character set is used as the default.
So you might want to try calling your function like this:
$output = htmlentities($input, ENT_COMPAT, 'UTF-8');
Does this solve your problem?

Hex to Unicode in PHP ( \u014D to ō) [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to decode Unicode escape sequences like “\u00ed” to proper UTF-8 encoded characters?
How can I convert \u014D to ō in PHP?
Thank You
It's not immediate clear what you mean when you say "to ō". If you're asking how to convert it into a different encoding then a general approach is to use the iconv function. 014D is the UCS-2 (unicode) for your desired function so, if you have a string containing the bytes 014D you could use
iconv('UCS-2', 'UTF-8', $s)
to convert from UCS-2 to UTF-8. Similarly if you want to convert to a different encoding - although you need to be aware that not all encodings will include the character you are using. You'll see from the iconv documentation that the //TRANSLIT option may help in that case.
Note that iconv is taking a byte sequence so, if you actually have a string containing a slash, then a u, then a 0 etc... you'll need to convert that into the byte sequence first.
If you have the escape characters in the string you could use a messy exec statement.
$string = '\\u014D';
exec("\$string = '$string'");
This way, the Unicode escape sequence should be recognized and interpreted as a unicode character When the string is parsed.
Of course, you should never use exec unless absolutely necessary.

Categories