开发者

HTML span tag content and attribute preg_match regular expressions

开发者 https://www.devze.com 2023-01-08 03:04 出处:网络
Does anyone have a fix for the following; I\'m taking the following string: <span id=\"tmpl_main_lblWord\" class=\"randomWord\">kid</span>

Does anyone have a fix for the following;

I'm taking the following string:

<span id="tmpl_main_lblWord" class="randomWord">kid</span>

and using the following preg_match / regex rule;

preg_match("'/(<spa开发者_开发百科n id=.* class=.*>)(.*)(<\/span>)/'si", $buffer, $match);

But its returning with an empty array any ideas?


The following example uses DOMDocument:

$doc = new DOMDocument();
$doc->loadHtml('<span id="tmpl_main_lblWord" class="randomWord">kid</span>');
$el = $doc->getElementById('tmpl_main_lblWord');
echo 'Inner text is: ' . $el->textContent;


In general I would strongly advise against using regex to try and get values from HTML. I would use an HTML parser. See this question: Robust and Mature HTML Parser for PHP

If you insist though... you seem to have two sets of nested quotes. I would remove the inner single quotes. That should solve your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消