我有一个jQuery脚本,它onClick Button从PHP文件中请求和导入值,并将这些值插入到表单中。
现在我可以成功地将值获取到正确的字段,但问题是值包含 HTML 代码,我只需要 TEXT 结果。
我需要字段的值:"种族",值应该是:"高加索人"
我尝试了不同的方法来查看$ethnicity
的值,有线的东西是:
echo $ethnicity; // result: Caucasian
var_dump($ethnicity); // result: string(146) " Caucasian "
$result['ethnicity'] = $ethnicity; // result: {"ethnicity":" Caucasian<'/td>'r"}
如您所见,$result值为:"Caucasian<'/td>'r"
我也试过:
$result['ethnicity'] = ($ethnicity->textContent); // result: It says "null"
我的问题是:如何从此值中删除此 HTML 代码?
对此有
strip_tagshttp://php.net/manual/en/function.strip-tags.php
$result['ethnicity'] = strip_tags($ethnicity);
通过使用filter_var()
和trim()
函数
$str = filter_var(trim($ethinicity,''t'n'r'0'x0B'), FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_LOW);
您可以使用
strip_tags
$ethinicity=strip_tags($ethinicity);
(或)
利用此功能 [来自 PHP 源代码手册]
<?php
function html2txt($document){
$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<['/'!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<!['s'S]*?--[ 't'n'r]*>@' // Strip multi-line comments including CDATA
);
$text = preg_replace($search, '', $document);
return $text;
}
$ethinicty=html2txt($ethinicity);
?>
编辑:
现在使用strip_tags它删除了",但"''r"仍然存在。现在 它看起来像:"高加索人''r"。我可以删除空格/空格吗 在白种人这个词之前也?
$ethnicity = str_replace(' 'r','',strip_tags($ethnicity));
$val = trim($val);
$val = strip_tags($val);
$val = htmlentities($val, ENT_QUOTES, 'UTF-8'); // convert funky chars to html entities
$pat = array("'r'n", "'n'r", "'n", "'r"); // remove returns
$val = str_replace($pat, '', $val);
$pat = array('/^'s+/', '/'s{2,}/', '/'s+'$/'); // remove multiple whitespaces
$rep = array('', ' ', '');
$val = preg_replace($pat, $rep, $val);
$val = trim($val);
$val = mysql_real_escape_string($val); // excellent final step for MySQL entry
没有一个函数来分隔字符串......
参考