简体   繁体   中英

PHP regular expression, get data part

I have HTML data, but I want to get a piece of this data. The top and bottom should be deleted. (everything after the H1 and above a H2 with text 'What we offer' should be put in a variable)

<p>This text can be deleted</p>
<h1>This title also</h1>

<h2>FROM THIS TITLE I WANT THE TEXT</h2><p>SAME HERE</p>
<h2>...</h2><p>...</p>

<h2>What we offer</h2>
<p>This text isn't needed</p>

I want all HTML and text beginning AFTER </h1> and ENDING at <h2>What we offer</h2> Any idea how to do this in PHP?

This does the trick without regexp (Thanks Alexandru), but I'm so curious what regexp I could use to achieve this...

$beginIndex = strpos($htmlString, "</h1>");
$endIndex = strpos($htmlString, "<h2>What we offer</h2>");
$desiredString = substr($htmlString, $beginIndex, $endIndex - $beginIndex);

Given the definition what you need, this should work:

$beginIndex = strpos($htmlString, "</h1>");
$endIndex = strpos($htmlString, "<h2>What we offer</h2>");
$desiredString = substr($htmlString, $beginIndex, $endIndex - $beginIndex);

The regex solution you are requesting would look something like this:

$pattern = '/<\/h1>(.*)<h2>What we offer/s';
$matches = array();
preg_match($pattern, $htmlString, $matches);
$desiredString = $matches[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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