I am writing some snippet script with PHP,
I have these kind of string variable
$msg="Dear [[5]]
We wish to continue the lesson with the [[6]]";
i need to grab 5 and 6 from this $msg and assign to an array ex array(5,6)
because those are the snippets numbers, any one know how to do that using PHP
thank you for help
Use preg_match_all
:
$msg = "Dear [[5]] We wish to continue the lesson with the [[6]]";
preg_match_all("/\[\[(\d+)\]\]/", $msg, $matches);
If there is a match, $matches[1]
will then contain an array with the matched numbers:
Array
(
[0] => 5
[1] => 6
)
DEMO.
Here is what you want:
<?php
$msg = "Dear [[5]]
We wish to continue the lesson with the [[6]]";
preg_match_all('/\[\[([0-9+])\]\]/', $msg, $array);
$array = $array[1];
print_r($array);
?>
Output:
Array
(
[0] => 5
[1] => 6
)