Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ' to an apostrophe in PHP

Tags:

My data has many HTML entities in it (• ...etc) including '. I just want to convert it to its character equivalent.

I assumed htmlspecialchars_decode() would work, but - no luck. Thoughts?

I tried this:

echo htmlspecialchars_decode('They're here.'); 

But it returns: They're here.

Edit:

I've also tried html_entity_decode(), but it doesn't seem to work:

echo html_entity_decode('They're here.') 

also returns: They're here.

like image 792
Dave Avatar asked Jul 22 '11 14:07

Dave


People also ask

How do you convert one currency to another?

The formula is: Starting Amount (Original Currency) / Ending Amount (New Currency) = Exchange Rate. For example, if you exchange 100 U.S. Dollars for 80 Euros, the exchange rate would be 1.25. But if you exchange 80 Euros for 100 U.S. Dollars, the exchange rate would be 0.8. Calculate the foreign currency amount.


1 Answers

Since ' is not part of HTML 4.01, it's not converted to ' by default.

In PHP 5.4.0, extra flags were introduced to handle different languages, each of which includes ' as an entity.

This means you can do something like this:

echo html_entity_decode('They're here.', ENT_QUOTES | ENT_HTML5); 

You will need both ENT_QUOTES (convert single and double quotes) and ENT_HTML5 (or any language flag other than ENT_HTML401, so choose the most appropriate to your situation).

Prior to PHP 5.4.0, you'll need to use str_replace:

echo str_replace(''', "'", 'They're here.'); 
like image 146
cmbuckley Avatar answered Sep 22 '22 01:09

cmbuckley