Convert ' to an apostrophe in PHP

25,335

Solution 1

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

Solution 2

There is a "right" way, without using str_replace , @cbuckley was right it's because the default for html_entity_decode is HTML 4.01, but you can set an HTML 5 parameter that will decode it.

Use it like this:

html_entity_decode($str,ENT_QUOTES | ENT_HTML5)

Solution 3

The ' entity and a lot of others are not in the PHP translation table used by html_entity_decode and htmlspecialchars_decode functions, unfortunately.

Check this comment from the PHP manual: http://php.net/manual/en/function.get-html-translation-table.php#73410

Solution 4

This should work:

$value = "They're here.";
html_entity_decode(str_replace("'","'",$value));

Solution 5

What you are actually looking for is html_entity_decode().

html_entity_decode() translates all entities to characters, while htmlspecialchars_decode() only reverses what htmlspecialchars() will encode.

EDIT: Looking at the examples on the page I linked to, I did a bit more investigation and the following seems to not work:

[matt@scharley ~]$ php
<?php
$tmp = array_flip(get_html_translation_table(HTML_ENTITIES));
var_dump($tmp['&apos;']);
PHP Notice:  Undefined index: &apos; in - on line 3
NULL

This is why it's not working. Why it's not in the lookup table is another question entirely, something I can't answer unfortunately.

Share:
25,335

Related videos on Youtube

Dave
Author by

Dave

Artist / Designer / Programmer

Updated on October 04, 2020

Comments

  • Dave
    Dave over 3 years

    My data has many HTML entities in it (&bull; ...etc) including &apos;. 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&apos;re here.');
    

    But it returns: They&apos;re here.

    Edit:

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

    echo html_entity_decode('They&apos;re here.')
    

    also returns: They&apos;re here.

  • Matthew Scharley
    Matthew Scharley almost 13 years
    @Dave: Unfortuanately, you may need to roll your own... Seems like PHP doesn't support reversing &apos; for some reason. See my edit for more.
  • cmbuckley
    cmbuckley almost 13 years
    And it seems that was because &apos; isn't part of HTML 4.01.
  • Matthew Scharley
    Matthew Scharley almost 13 years
    @cbuckley sound like a likely reason. &apos; was pulled into XHTML for compatibility with XML.
  • Dave
    Dave over 11 years
    I appreciate your time, but this is the same as the already-approved answer.
  • cmbuckley
    cmbuckley about 10 years
    I've updated the answer for PHP 5.4.0 (props to eric.itzhak for the update).