Jump to content

[SOLVED] Store preg match all results in one variable??


john_bboy7

Recommended Posts

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..

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.

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.

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.