0

Please, i want to get every string between two same element (only strong, not between strong and /strong).

Example string:

<strong>NAME1</strong><br />Some text, some text<br />
<strong>NAME2</strong><br />Some text2, some text2<br />
<strong>NAME3</strong><br />Some text3, some text3<br />

I try this one:

preg_match_all("'<strong>(.*?)<strong>'si", $text, $match);

but result is:

<strong>NAME1</strong><br />Some text, some text<br /><strong>
<strong>NAME3</strong><br />Some text3, some text3<br />

,,NAME2" with ,,some text2" missing, why?

Thank's a lot, Regards, Lukas

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
striky
  • 1
  • 1

1 Answers1

0

THE PONY HE COMES

Now that that's out of the way, how about we get some parser code in here, hmm?

$dom = new DOMDocument();
$dom->loadHTML($text);
$bodychlldren = $dom->getElementsByTagName('body')->item(0)->childNodes;
$outputtags = array();
$currentTag = null;
$count = $bodychildren->length;
for( $i=0; $i<$l; $i++) {
    $node = $bodychildren->item($i);
    if( strtolower($node->nodeName) == "strong") {
        if( $currentTag !== null) $outputtags[] = $currentTag;
        $currentTag = "";
    }
    if( $node->nodeType == XML_ELEMENT_NODE) $currentTag .= $dom->saveHTML($node);
    else $currentTag .= $node->nodeValue;
}
if( $currentTag) $outputtags[] = $currentTag;

That's actually uglier than I thought it would be. Huh.

If you really want to use a regex, you might try this:

"(<strong>(.*?)(?=<strong>))si"

This will look for the following <strong>, but not include it in the match, thereby allowing NAME2 to appear.

Community
  • 1
  • 1
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592