質問

Can you use strpos() searching for HTML tags? Seems to produce invalid results. Also tried converting to htmlentities() -- still no luck. How can I properly search for text decorations like: bold, italics, and underline?

Example: (demo)

/* 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);
役に立ちましたか?

解決

You got the order of the parameters wrong for stripos, it should be haystack, then needle...

if(stripos($value,$html_tag['before']) !== false && strripos($value,$html_tag['after']) !== false){

他のヒント

Why would you want to do this with RegEx? Your best bet is DOM.

RegEx match open tags except XHTML self-contained tags

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top