如何使用php获取字符串中html的特定属性? [英] How to get specific attribute of html in string using php?
本文介绍了如何使用php获取字符串中html的特定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了一个字符串,我需要找出所有 data-id
数字.这是字符串
I got a string and I need to find out all the data-id
numbers.
This is the string
<li data-type="mentionable" data-id="2">bla bla...
<li data-type="mentionable" data-id="812">some test
<li>bla bla </li>more text
<li data-type="mentionable" data-id="282">
所以最后它会找到我:2,812,282
推荐答案
您可以使用正则表达式在 preg_match_all()
.
You can use regex to find target part of string in preg_match_all()
.
preg_match_all("/data-id=\"(\d+)\"/", $str, $matches);
// $matches[1] is array contain target values
echo implode(',', $matches[1]) // return 2,812,282
在演示
因为你的字符串是HTML,你可以使用DOMDocument
类来解析 HTML 并在文档中查找目标属性.
Because your string is HTML, you can use DOMDocument
class to parse HTML and find target attribute in document.
这篇关于如何使用php获取字符串中html的特定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文