您可以使用strpos()
搜索 HTML 标记吗?似乎产生无效的结果。还尝试转换为htmlentities()
- 仍然没有运气。如何正确搜索文本装饰,例如:粗体、斜体和下划线?
示例:(演示)
/* HTML Tags to search for. */
$html_tags = array(
'bold' => array(
'before' => '<strong>',
'after' => '</strong>'
),
'italics' => array(
'before' => '<em>',
'after' => '</em>'
),
'underline' => array(
'before' => '<span style="text-decoration: underline;">',
'after' => '</span>'
)
);
/* Sample Strings... */
$html_test = array(
'bold_with_html' => '<strong>Some string containing HTML tags.</strong>',
'italics_with_html' => '<em>Some string containing HTML tags.</em>',
'underline_with_html' => '<span style="text-decoration: underline;">Some string containing HTML tags.</span>',
'without_html' => 'Some string containing no HTML tags.'
);
/* Check for HTML Tags. */
$results = array();
foreach($html_test as $key => $value){
foreach($html_tags as $decoration => $html_tag){
if(stripos($html_tag['before'], $value) !== false && strripos($html_tag['after'], $value) !== false){
$results[$key][$decoration] = 'Located HTML: '.$decoration.'!';
} else{
$results[$key][$decoration] = 'No HTML located.';
}
}
}
print_r($results);