Php 如何检查段落或段落末尾是否有img标签

Php 如何检查段落或段落末尾是否有img标签,php,regex,preg-match,Php,Regex,Preg Match,我如何检查是否只有在段落中或段落末尾有img标签 $input = '<p><img src="#" /></p>'; // ok $input = '<p><img src="#" /><img src="#" /></p>'; // ok $input = '<p>xxx<img src="#" /></p>'; // ok $input = '<p><

我如何检查是否只有在段落中或段落末尾有img标签

$input = '<p><img src="#" /></p>'; // ok
$input = '<p><img src="#" /><img src="#" /></p>'; // ok
$input = '<p>xxx<img src="#" /></p>'; // ok

$input = '<p><img src="#" />xxx</p>'; // NOT ok

if(preg_match("/img/$", $input) { ... };
$input='

';//好啊 $input='

';//好啊 $input='xxx

';//好啊 $input='xxx

';//不好 如果(preg_match(“/img/$”,$input){…};
除了
preg\u match
regex
之外,还有其他更好的方法吗?

您可以使用,并检查
节点上是否有任何文本内容:

$doc = new DOMDocument;
$doc->loadHTML( $input);

$p = $doc->getElementsByTagName('p')->item(0);
$children = $p->childNodes;

$img_found = false; $error = false;

foreach( $children as $child) {
    if( $child->nodeType == XML_TEXT_NODE && !$img_found) {
        // Text before the image, OK, continue on
        continue;
    }
    if( $child->nodeType == XML_ELEMENT_NODE) {
        // DOMElement node
        if( !($child->tagName == 'img')) {
            // echo "Invalid HTML element found";
            $error = true;
        } else {
            $img_found = true;
        }
    } else {
        // echo "Invalid node type!";
        $error = true;
    }
}

// No errors and we found at least one image, we're good
if( $img_found && !$error) {
    echo 'ok' . "\n";
} else {
    echo 'NOT ok' . "\n";
}
从中可以看出,is通过了所有测试。它满足以下要求:

  • 只有
    标记内的图像和文本无效。任何其他标记都无效。如果不正确,请根据需要修改它
  • 如果有文本,它必须在我们检测到
    标记之前出现
  • 您可以使用并检查
    节点上是否有任何文本内容:

    $doc = new DOMDocument;
    $doc->loadHTML( $input);
    
    $p = $doc->getElementsByTagName('p')->item(0);
    $children = $p->childNodes;
    
    $img_found = false; $error = false;
    
    foreach( $children as $child) {
        if( $child->nodeType == XML_TEXT_NODE && !$img_found) {
            // Text before the image, OK, continue on
            continue;
        }
        if( $child->nodeType == XML_ELEMENT_NODE) {
            // DOMElement node
            if( !($child->tagName == 'img')) {
                // echo "Invalid HTML element found";
                $error = true;
            } else {
                $img_found = true;
            }
        } else {
            // echo "Invalid node type!";
            $error = true;
        }
    }
    
    // No errors and we found at least one image, we're good
    if( $img_found && !$error) {
        echo 'ok' . "\n";
    } else {
        echo 'NOT ok' . "\n";
    }
    
    从中可以看出,is通过了所有测试。它满足以下要求:

  • 只有
    标记内的图像和文本无效。任何其他标记都无效。如果不正确,请根据需要修改它
  • 如果有文本,它必须在我们检测到
    标记之前出现

  • 请尝试以下正则表达式:

    /<p[^>]*>.*<img[^>]*></p>/
    
    /]*>.*>

    /
    说明:
    如果要将具有未知属性的标记与正则表达式匹配,可以使用
    ]*>
    。表达式
    [^>]*
    将除
    之外的每个字符匹配零次或n次,从而接受任意数量的属性。

    尝试此正则表达式:

    /<p[^>]*>.*<img[^>]*></p>/
    
    /]*>.*>

    /
    说明: 如果要将具有未知属性的标记与正则表达式匹配,可以使用
    ]*>
    。表达式
    [^>]*
    将除
    之外的每个字符匹配零次或n次,从而接受任意数量的属性。

    使用此表达式:

    if (preg_match("/<img\s[^<>]*><\/p>/i", $input) { ... };
    
    if(preg_match(“//i”,$input){…};
    
    使用这个:

    if (preg_match("/<img\s[^<>]*><\/p>/i", $input) { ... };
    
    if(preg_match(“//i”,$input){…};
    
    您自己第一次尝试了吗?您提供的正则表达式甚至不接近。您自己第一次尝试了吗?您提供的正则表达式甚至不接近。比正则表达式好很多,但遗憾的是它没有捕捉到第三个示例(文本后接图像)。这并没有打折其他非文本HTML元素(我想不出任何可能出现在p标签中的东西:p),并且没有考虑到其他元素只要在p标记的末尾就可以正常工作的事实。@Jeroen-我的第一个答案很快,因为我的测试服务器处于脱机状态,我已经用代码板演示更新了我的答案。@Maccath-它现在考虑所有节点,但是任何不是文本节点和
    标记的节点都是无效的根据OP.Way提供的规范,它比正则表达式更好,但不幸的是,它没有捕捉到第三个示例(文本后跟图像)。这并没有打折其他非文本HTML元素(我想不出任何可能出现在p标记中的元素:p),并且没有考虑到其他元素只要在p标记的末尾就可以正常工作的事实。@Jeroen-我的第一个答案很快,因为我的测试服务器处于脱机状态,我已经用代码板演示更新了我的答案。@Maccath-它现在考虑所有节点,但是任何不是文本节点和
    标记的节点都是无效的根据OP提供的规范。