You can do it without using Regex by using string replace function (str_replace()
):
<?php
$name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
$myArra = array();
foreach ($name as $key => $value) {
$var = str_replace("page", "", $value);
$var = str_replace(".jpg", "", $var);
$myArra[] = $var; // store all value without text and .jpg
}
print_r($myArra);
?>
Result:
Array
(
[0] => 18
[1] => 16
[2] => 17
[3] => 19
)
Or, if you want to use Regex, than you can use /\d+/
pattern as like:
<?php
$name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
$myArra = array();
foreach ($name as $key => $value) {
preg_match_all('/\d+/', $value, $matches);
$myArra[] = $matches[0][0]; // store all value without text and .jpg
}
echo "<pre>";
print_r($myArra);
?>
Result:
Array
(
[0] => 18
[1] => 16
[2] => 17
[3] => 19
)