Scummy12 Posted September 2, 2010 Share Posted September 2, 2010 If I have got the file contents of a page and it appears like this: <id>18800<end> <id>18436<end> etc.... How can I split the data and add the numeric values into an array, and then perform a function on all the elements in the array? Quote Link to comment https://forums.phpfreaks.com/topic/212338-add-to-array/ Share on other sites More sharing options...
sasa Posted September 2, 2010 Share Posted September 2, 2010 <?php $test = '<id>18800<end> <id>18436<end>'; preg_match_all('/<id>(\d+)<end>/',$test, $out); print_r($out[1]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/212338-add-to-array/#findComment-1106385 Share on other sites More sharing options...
JonnoTheDev Posted September 2, 2010 Share Posted September 2, 2010 I would write a little function that you can reuse. <?php /* function to match all from given start & end point return results as array */ function parseArray($string, $openTag, $closeTag, $excluding = false) { preg_match_all("($openTag(.*)$closeTag)siU", $string, $matches); if($excluding) { return $matches[1]; } return $matches[0]; } /* usage */ $string = "<id>18800<end><id>18436<end>"; $results = parseArray($string, "<id>", "<end>", true); print "<pre>"; print_r($results); print "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/212338-add-to-array/#findComment-1106386 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.