![](/img/trans.png)
[英]PHP replace parts of a string and get the those replaced parts as separate strings
[英]How to separate a string into two, and then make one of those strings into a number [php]
我有这个字符串:
"$string="Med0x2"
我想要的是将其分成两个字符串,如下所示:
$string1="Med0x" ; $string2="2";
然后将$string2
转换为int变量(从string转换为int)
我该怎么做呢?
$string = "Med0x2";
$string1 = substr($string, 0, 5);
$string2 = substr($string, 5);
$integer = (int) $string2;
您可以为此使用preg_match
,使用捕获组来获取字符串的各个部分。
像这样-
$string = "Med0x2";
$regex = "/^(.*?x)(\d+)$/";
if(preg_match($regex, $string, $matches)){
$string1 = $matches[1];
$string2 = $matches[2];
$integer = intval($string2);
}
您需要在第一个VA中查找子字符串
$string1 = "Med0x2";
$string2 = (int) substr($string1, -1);
echo "$string2";
echo is_int($string2) ? "yes":"no";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.