php - 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 标记?
解决方案
使用 DOMDocument:
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($content->description);
if ( $dom->getElementsByTagName('a')->length > 1 ) {
...
推荐阅读
- arrays - 将字典转换为数组
迅速 - javascript - 使用ajax上传多个文件
- c++ - 我正在尝试定义一个只能在堆上实例化的类
- android - 使用 GridLayoutManager 和多个项目类型(具有不同高度)滚动 RecyclerView 时发生滞后
- python - Watson 生成的 Pytorch 结果:“ValueError: optimizer got an empty parameter list”
- python - 使用 Google Photos API 时可以上传照片
- azure - Azure ARM 模板:如何使用模板创建 vnet 并将其与现有 vnet 对等?
- c++ - 寻找更紧凑的语法(简单代码) - C++
- cassandra - 如何在 DSE 6 中使用或启用密钥缓存(密钥缓存)
- .net - 在值不等于的情况下如何使用数据触发器