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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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"; } } Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.