首页 > 解决方案 > PHP:preg_match 限制表单文本区域中的锚文本

问题描述

我正在尝试将文本区域中的锚文本数量限制为 1。

经过几周的研究,我无法使用此代码做到这一点

    if(preg_match('/.*http:\//', $content->description) > 1) {
    else 'You can not submit Anchor text more than one times';
}

<textarea>
<a href="https://example.com/">Anchor text 1</a>
<a href="http://example2.com/">Anchor text 2</a>
<a href="https://www.example3.com/">Anchor text 3</a>
<a href="http://www.example4.com/">Anchor text 4</a>
Not Anchor text http://www.example.com/
</textarea>

如何<a>使用“preg_match”限制表单文本区域中的提交 HTML 标记?

标签: phppreg-match

解决方案


使用 DOMDocument:

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($content->description);

if ( $dom->getElementsByTagName('a')->length > 1 ) {
    ...

推荐阅读