Php - regex for retrieving content in div tags

Hi, thanks for looking at my question. I need to collect some data from an HTML fragment. This source is reliable / structured, so I think it's for using regex in this HTML. With home and other advanced features in php, I think this is overkill.

Here is the HTML snippet format.

<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 the example above, note that the first two DIV tags have identifiers set. There may be several custom_1 lines, such as div tags, so I will need to avoid them.

Actually, I am very poor in regular expression, so I expect help from you to help John Smith from the top to bottom of the html fragment.

It could be something like

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

, regex. John Smith html. , html .

, HTML . .

30 : HTML, ; . , - , , DOM .

+3
1

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

:

User Name: [John Smith]
+5

All Articles