john_bboy7 Posted April 21, 2009 Share Posted April 21, 2009 Store preg match all results in one variable?? i am trying it for a while now.. even googled it but still not getting it. Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/ Share on other sites More sharing options...
premiso Posted April 21, 2009 Share Posted April 21, 2009 preg_match preg_match_all The third parameter is the array to store the matches. That puts them all in "one" variable already. Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/#findComment-815665 Share on other sites More sharing options...
john_bboy7 Posted April 21, 2009 Author Share Posted April 21, 2009 preg_match The third parameter is the array to store the matches. That puts them all in "one" variable already. But preg match stops after first match. And i want to save it as like $save = $matches[0]; then use $save in my further codes.. Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/#findComment-815666 Share on other sites More sharing options...
nrg_alpha Posted April 21, 2009 Share Posted April 21, 2009 preg_match_all. Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/#findComment-815670 Share on other sites More sharing options...
premiso Posted April 21, 2009 Share Posted April 21, 2009 Sorry, I meant to refer you to preg_match_all int preg_match_all ( string $pattern , string $subject , array &$matches [, int $flags [, int $offset ]] ) See the examples for ways the matches array is output and can be utilized. Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/#findComment-815671 Share on other sites More sharing options...
john_bboy7 Posted April 21, 2009 Author Share Posted April 21, 2009 Sorry, I meant to refer you to preg_match_all int preg_match_all ( string $pattern , string $subject , array &$matches [, int $flags [, int $offset ]] ) See the examples for ways the matches array is output and can be utilized. Yeah i am also trying that way but i am having results like more than 20 or 30 and every time different.. I used the count option.. but i am not sure how to save all the result in one single variable.. as i mention above $save = $matches[0]; then i can use the $save. Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/#findComment-815679 Share on other sites More sharing options...
nrg_alpha Posted April 21, 2009 Share Posted April 21, 2009 Here is a silly example for illustrative purposes: $str = 'Text: 1 2 3 4 more text! 5'; $save = ''; preg_match_all('#\d#', $str, $matches); foreach($matches[0] as $val){ $save .= $val . ' '; } echo $save; // ouput: 1 2 3 4 5 The . ' ' at the end of the save line appends a space so that the characters extracted from the array $matches[0] are not all bunched up together. So adapt that snippet to whatever you are using.. if you don't want the space in between, simply get rid of . ' ' (or substitute the space for something else if you want, like - or _ or whatever). Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/#findComment-815705 Share on other sites More sharing options...
premiso Posted April 21, 2009 Share Posted April 21, 2009 implode would probably be a lot easier to do Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/#findComment-815707 Share on other sites More sharing options...
nrg_alpha Posted April 21, 2009 Share Posted April 21, 2009 Yeah, it would. lol Don't mind me today.. I'm a little off Link to comment https://forums.phpfreaks.com/topic/155075-solved-store-preg-match-all-results-in-one-variable/#findComment-815710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.