【问题标题】:How can I detect a html tag on a string with php?如何使用 php 检测字符串上的 html 标签?
【发布时间】:2017-05-28 17:21:31
【问题描述】:

我需要知道html中有一个字符串是什么标签,例如:

$string = '<a href="http://example.com/">Hello World!</a>'
$result= 'a';

PHP中有函数吗?

【问题讨论】:

  • 您是否需要知道何时标签出现在字符串中,或​​者从字符串中删除标签?
  • 我需要知道标签是什么(对不起我的英语)

标签: php html yii2


【解决方案1】:

你可以使用 preg_match :

 $result = preg_match("/<[^<]+>/",$string, $res);

【讨论】:

    【解决方案2】:

    您可以使用 DOMDocument (http://php.net/manual/en/class.domdocument.php) 从 HTML 字符串构建 DOM,然后逐个循环遍历每个元素以查找标签。

    请参阅此答案 (https://stackoverflow.com/a/18800862/3002418) 作为示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      • 2011-08-21
      • 1970-01-01
      • 2013-09-12
      • 2012-10-18
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多