I have a string like this : x320h220
I want to split this string into two variables : $width
and $height
I Can split it using explode but I have to use this method twice to get these values so I found out preg_split
will do the trick. But I don't know the Regex to split these string into two variables (find if string contain x then cut the rest till h and then cut the rest again in another variable) which they should be like :
$width = 320;
$height = 220;
any suggestion ?
Use preg_match()
, not preg_split()
. Use capture groups to extract the two numbers.
if (preg_match('/x(\d+)h(\d+)/', $string, $match) ) {
$width = $match[1];
$height = $match[2];
}
In the regexp:
x
and h
match themselves literally \\d+
matches a sequence of digits ()
around the digit sequence creates a capture group, so the matched portions are put into the $match
array. use this $size = preg_split("/[xh]/", "x320h220", -1, PREG_SPLIT_NO_EMPTY)
Hope it will work for you :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.