简体   繁体   English

如何在ID为1st属性的div标签之间获取文本。 仅带有正则表达式。

[英]How to get text between div tags with id being 1st attribute. Only with regular expressions.

Here is the scenario 这是场景

I'm using this regular expression for getting the text inside div tag with id test 我正在使用此正则表达式使用id test获取div标签内的文本

<div id = "test">text</div>

$regex = "#\<div id=\"test\"\>(.+?)\<\/div\>#s";

My only requirement is to get the text from div tags in below scenario 我唯一的要求是在以下情况下从div标签获取文本

<div id="test" class="teating" style="color:red" etc etc .... more attributes >

ie id being the 1st attribute of div tag and can be followed by n number of attributes. 即id是div标签的第一个属性,并且可以跟随n个属性。 How to extract text from such a tag by regex only. 如何仅通过正则表达式从此类标记中提取文本。 Please help Thanks You. 请帮助谢谢。

使用此正则表达式:

$regex = '#<div\s+.*?id="test".*?>(.*?)</\s*div>#ims';

in php: with $replacement defined as your input string. 在php中:$ replacement定义为您的输入字符串。

$pattern = '#<div[^/>]*>([^<]*)</div>#';
$replacement = '\1';
$text = preg_replace($pattern, $replacement, $subject);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM