Using WordPress ‘ent2ncr()’ PHP function

The ent2ncr() WordPress PHP function converts named entities into numbered entities within a given text.

Usage

Here’s an example of how you could use the ent2ncr() function:

$text = "C’est la fête!";
echo ent2ncr($text); 

This outputs: C’est la fête!

Parameters

  • $text (string) – This is the text within which entities will be converted.

More Information

See WordPress Developer Resources: ent2ncr()

This function is a part of WordPress Core and is included in all versions of WordPress since 2.8.0. It has not been deprecated and is an integral part of the WordPress HTML API.

Examples

Basic Usage

This is a simple usage of ent2ncr() function.

$text = "Léon"; // French name with a named entity
echo ent2ncr($text); 

This will output: Léon.

Converting Multiple Entities

In this example, we convert multiple named entities in a sentence.

$text = "J’aime les crêpes"; // I love crepes in French
echo ent2ncr($text); 

This will output: J’aime les crêpes.

Using with HTML Tags

Here we’re showing how ent2ncr() works with HTML tags.

$text = "<p>Bonjour, c&amp;rsquo;est moi!</p>"; // Hello, it's me in French
echo ent2ncr($text); 

This will output: <p>Bonjour, c&#8217;est moi!</p>.

Using with Special Characters

This example demonstrates how ent2ncr() handles special characters.

$text = "M&amp;icirc;nous, l&amp;rsquo;&amp;eacute;l&amp;egrave;ve!"; // Go, the student! in French
echo ent2ncr($text); 

This will output: M&#238;nous, l&#8217;&#233;l&#232;ve!.

Using with Non-ASCII Characters

This example shows how ent2ncr() function can also handle non-ASCII characters.

$text = "C&amp;rsquo;est la f&amp;ecirc;te!"; // It's a party! in French
echo ent2ncr($text); 

This will output: C&#8217;est la f&#234;te!.