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? 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]); ?> 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/212338-add-to-array/#findComment-1106386 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.