0

我有一个 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 代码?

4

4 回答 4

2

该http://php.net/manual/en/function.strip-tags.php有 strip_tags

$result['ethnicity'] = strip_tags($ethnicity);
于 2013-11-07T10:59:09.667 回答
0

你可以利用strip_tags

$ethinicity=strip_tags($ethinicity);

(或者)

使用这个函数 [来自 PHP Source Manual]

<?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 它删除了 "</td>" 但 "\r" 仍然存在。现在它看起来像:“Caucasian\r”。我也可以删除单词 Caucasian 之前的空格/空格吗?

  $ethnicity = str_replace(' \r','',strip_tags($ethnicity));
于 2013-11-07T10:58:58.490 回答
0

通过使用filter_var()trim() 功能

$str = filter_var(trim($ethinicity,'\t\n\r\0\x0B'), FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_LOW);
于 2013-11-07T11:03:01.817 回答
0
$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

没有单独的函数来分隔字符串............

参考

于 2013-11-07T11:38:36.180 回答