Jump to content

Looping with xpath


blinks

Recommended Posts

The following code works nicely if there is one topic node, but not if there are multiple nodes. I know I need a foreach loop. Can anyone help?

	$fieldNodeList = $xpath->query("/subject/topic");
	$currentString = $fieldNodeList->item(0)->nodeValue;
	if (preg_match('/^keyword2/', $currentString)) {
		$newString = "key word 2";
		$fieldNodeList->item(0)->nodeValue = $newString;
	}

 

TIA

Link to comment
https://forums.phpfreaks.com/topic/191836-looping-with-xpath/
Share on other sites

Got it, thank you! Just dropped the ampersand in front of the first $node, as per -

 

$fieldNodeList = $xpath->query("/subject/topic");

foreach($filedNodeList as $node) {

      if (preg_match('/^keyword2/', $node->nodeValue)) {

        $node->nodeValue = "key word 2";

      }

}

Link to comment
https://forums.phpfreaks.com/topic/191836-looping-with-xpath/#findComment-1011148
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.