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);