简体   繁体   中英

PHP - How to get the text from multiple p tags in a row

I have a variable holding some html. In the html there are several p tags next to each other. I'd like to retrieve the text from those p tags and format them into 1 paragraph and set this new paragraph to a new variable. Does that make sense?

<h2></h2>
<p>date</p>
<ul></ul>
<iframe></iframe>
<p>apple</p>
<p>banana</p>
<p>grape</p>
<ul></ul>

becomes:

<p>apple banana grape</p>

NOTE: the elements in the html may vary a bit. No iframe for example. An image somewhere, etc.

You can use Xpath to do that

$html = '<h2></h2>
<p>date</p>
<ul></ul>
<iframe></iframe>
<p>apple</p>
<p>banana</p>
<p>grape</p>
<ul></ul>';

$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXpath($doc);

$elements = $xpath->query("//p");
foreach ($elements as $node){
echo $node->nodeValue. "<br/>";
}

Example:

$html = '<h2></h2>
  <p>date</p>
  <ul></ul>
  <iframe></iframe>
  <p>apple</p>
  <p>banana</p>
  <p>grape</p>
  <ul></ul>';

if (preg_match_all('~<p>(?P<paragraphs>.*?)</p>~is', $html, $matches)) {
  print_r($matches['paragraphs']);
}

Result:

Array
(
    [0] => date
    [1] => apple
    [2] => banana
    [3] => grape
)

PS "Smart" downvoters could you please comment why are you downvoting? :))))))))))))))

PPS Special edition for those two who downvoted:

$document = new DOMDocument();
$document->loadHTML($html);
$paragraphs = $document->getElementsByTagName('p');
$result     = array();

foreach ($paragraphs as $paragraph) {
  $result[] = $paragraph->nodeValue;  
}

print_r($result);

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