Search code examples
phppreg-replacestr-replacehtmlspecialchars

htmlspecialchars is not working as I want! I want < to be gt, NOT &gt;


This is my input:

<

This is the output I need:

gt

But this is the output I GET from htmlspecialchars:

&gt;

Basically, I want htmlspecialchars result, but really stripped down. I only want characters a-z and 0-9.

Can anyone help?


Solution

  • This should work for you:

    (Just preg_replace() all characters which aren't in a-z0-9 with an empty string which you get from htmlentities())

    <?php
    
        $str = "<";
        echo preg_replace("/[^a-z0-9]/", "", htmlentities($str));
    
    ?>
    

    output:

    lt