使用php自带的DOMDocument来进行数据处理
在Common公共方法中添加代码
if (!function_exists('getTextToArray')) {
/**
* 把富文本转为数组获取关键词
* @param $text
*/
function getTextToArray($text)
{
if (empty($text)) {
return [];
}
$dom = new DOMDocument();
libxml_use_internal_errors(true); // 禁用错误报告
$dom->loadHTML($text, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
libxml_clear_errors(); // 清除错误
$nodes = $dom->getElementsByTagName('*');
$nodeArray = [];
foreach ($nodes as $node) {
$content = $node->nodeValue;
$tagName = $node->tagName;
$attributes = [];
// 获取所有属性
foreach ($node->attributes as $attribute) {
$attributes[$attribute->name] = $attribute->value;
}
$nodeArray[] = [
'tag' => $tagName,
'content' => $content,
'attributes' => $attributes
];
}
return $nodeArray;
}
}
在控制器直接调用
根据项目获取对应tap==p的content的值