简体   繁体   English

php - 正则表达式获取DIV标签中的内容

[英]php - regex to get contents in DIV tags

Hello and thank for looking at my question. 您好,感谢您查看我的问题。 I'm in need to grab some data from an HTML snippet. 我需要从HTML片段中获取一些数据。 This source is a trusted/structured one so I think it's OK to use regex in this HTML. 这个源是一个受信任的/结构化的,所以我认为在这个HTML中使用正则表达式是可以的。 Dom and other advanced features in php are an overkill I guess. 我认为Dom和php中的其他高级功能是一种矫枉过正。

Here is the format of the HTML snippet. 以下是HTML代码段的格式。

<div id="d-container">
  <div id="row-custom_1">
     <div class="label">Type</div>
     <div class="content">John Smith</div>
     <div class="clear"></div>
  </div>
</div>

In above, please note the first 2 DIV tags have IDs set. 在上面,请注意前2个DIV标签设置了ID。 There could be several row-custom_1 like div tags so I will need to escape them. 可能有几个row-custom_1像div标签,所以我需要逃避它们。

I'm actually very poor in regex so I'm expecting a help from you to rab the John Smith from above html snippet. 我的正则表达式实际上非常差,所以我期待你的帮助,从上面的html片段中抓住约翰史密斯。

It could be something like 它可能是这样的

<div * id="row-custom_1" * > * <div * class="content" * >GRAB THIS </div>

but I don't know how to do it in regex. 但我不知道如何在正则表达式中做到这一点。 John Smith part won't contain any html for sure. 约翰史密斯部分肯定不会包含任何HTML。 it's from a trusted source that it strips all html and gives the data in above format. 它来自一个值得信赖的来源,它会删除所有html并以上述格式提供数据。

I can understand that regex is never a good idea to process HTML anyway. 我可以理解正则表达式无论如何都不是处理HTML的好主意。 Thank you very much for any assistance. 非常感谢你的帮助。

Edit just after 30 minutes: Many of the awesome people suggested to use an HTML parser so I did ; 30分钟后编辑:很多很棒的人建议使用HTML解析器,所以我做了; worked like a charm. 像魅力一样工作。 So if anyone comes here with a similar question, as the stupid question author, I'd recommend using DOM for the job. 因此,如果有人带着类似的问题来到这里,作为愚蠢的问题作者,我建议使用DOM来完成工作。

Here is a simple DOM based code to get your value from the given HTML: 这是一个简单的基于DOM的代码,用于从给定的HTML中获取您的值:

$html = <<< EOF
<div id="d-container">
  <div id="row-custom_1">
     <div class="label">Type</div>
     <div class="content">John Smith</div>
     <div class="clear"></div>
  </div>
</div>
EOF;
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$xpath = new DOMXPath($doc);
$value = $xpath->evaluate("string(//div[@id='d-container']
         /div[@id='row-custom_1']/div[@class='content']/text())"); 
echo "User Name: [$value]\n"; // prints your user name

OUTPUT: OUTPUT:

User Name: [John Smith]

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

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