Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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