Seen in stackoverflow:
What is the easiest way to encode a PHP string for output to a Javascript variable?
I have a PHP string which includes quotes and newlines. I need the contents of this string to be put into a Javascript variable.
Normally, I would just construct my Javascript in a PHP file, ala:
var myvar = "";
However, this doesn't work when $myVarValue contains quotes or newlines.
Expanding on someone else's answer:
This does require PHP 5.2.0 or greater.
this is just for strip the inside tags
$allow = '
- ';
$str = 'ParagraphBold
RedHeader
';
$result = strip_tags($str,$allow);
$result = clean_inside_tags($result,$allow);
echo '';
//Clean the inside of the tags
function clean_inside_tags($txt,$tags){
preg_match_all("/<([^>]+)>/i",$tags,$allTags,PREG_PATTERN_ORDER);
foreach ($allTags[1] as $tag){
$txt = preg_replace("/<".$tag."[^>]*>/i","<".$tag.">",$txt);
}
return $txt;
}
?>