blinks Posted February 12, 2010 Share Posted February 12, 2010 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 More sharing options...
JAY6390 Posted February 12, 2010 Share Posted February 12, 2010 $fieldNodeList = $xpath->query("/subject/topic"); foreach($filedNodeList as &$node) { if (preg_match('/^keyword2/', $node->nodeValue)) { $node->nodeValue = "key word 2"; } } Untested but should work Link to comment https://forums.phpfreaks.com/topic/191836-looping-with-xpath/#findComment-1011143 Share on other sites More sharing options...
blinks Posted February 12, 2010 Author Share Posted February 12, 2010 Thanks for your reply. Have set the code as per your specs, but it is not throwing up the following error - Fatal error: An iterator cannot be used with foreach by reference in Link to comment https://forums.phpfreaks.com/topic/191836-looping-with-xpath/#findComment-1011146 Share on other sites More sharing options...
blinks Posted February 12, 2010 Author Share Posted February 12, 2010 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 More sharing options...
JAY6390 Posted February 12, 2010 Share Posted February 12, 2010 Oops, of course that will work lol. Don't need the ampersand with objects. Minds a bit outta touch since it's gone 3am. Think this is a way of telling myself it's bed time Link to comment https://forums.phpfreaks.com/topic/191836-looping-with-xpath/#findComment-1011150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.