Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters to HTML ASCII Entity Equivalent

How will I convert all special characters to their corresponding html entity?

Special character would be like $ & / \ { } ( - ' , @ etc.

I tried to use htmlentities() and htmlspecialchars(). But didn't solve my problem.

please check here. I want output like Entity Number i.e. column 3.

Actually the scenario is - I need to take input from fckeditor. and then save into the database. So I need to convert all special character to their corresponding html entity, from the text. Otherwise it's giving me error.

like image 619
Ripa Saha Avatar asked Nov 10 '22 14:11

Ripa Saha


1 Answers

What you are looking is for an ASCII equivalent of a character. So you need to make use of ord().

By the way what divaka mentioned is right.

Do like this..

<?php

function getHTMLASCIIEquiv($val)
{
    $arr=['$','&','/','\\','{','}','(','-','\'',',','@'];
    $val = str_split($val);$str="";
    foreach($val as $v)
    {
        if(in_array($v,$arr))
        {
        $str.="&#".ord($v).";";
        }
        else
        {
            $str.=$v;
        }
    }
    return $str;
}

echo getHTMLASCIIEquiv('please check $100 & get email from [email protected]');

OUTPUT :

please check &#36;100 &#38; get email from test&#64;cc.com

Demo

like image 93
Shankar Narayana Damodaran Avatar answered Nov 14 '22 23:11

Shankar Narayana Damodaran