1

How to retrieve the contents of the HTML tag.

    $foo = '<p>

aaaaaa                </p>

<p>            
bbbbbb bb 
bbbbbbb  </p>
         <p>            cccccccc 
ccc</p>';

I need to get "aaaaaa"

thank you very much

Z55
  • 85
  • 5
  • 3
    possible duplicate of [PHP/regex: How to get the string value of HTML tag?](http://stackoverflow.com/questions/828870/php-regex-how-to-get-the-string-value-of-html-tag) – 0x9BD0 Apr 02 '14 at 15:35
  • See this awsner using PHP Simple HTML DOM Parser http://stackoverflow.com/a/1364269/3448695 – sergiovilar Apr 02 '14 at 20:45

2 Answers2

2

Use a DOMParser

echo trim($dom->getElementsByTagName('p')->item(0)->nodeValue); //"prints" aaaaaa

Demo

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

Use this:

$content = null ;
if ( preg_match("/<p[^>]*>(.*?)<\\/p>/si", $foo, $match) )
    $content = $match[1]; 

use $match[2] for the second match, etc.

Roemer
  • 1,124
  • 8
  • 23