Reputation: 2540
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.
Upvotes: 5
Views: 600
Reputation: 68526
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 $100 & get email from test@cc.com
Upvotes: 1