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.
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
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With