问题
My current project consist of UID in which it looks like follows
855FM21 , 855FM22 ,etc
i want the last number from the uid in which it can be done by splitting the string.
How to split this string after the sub string "FM" .?
回答1:
To split this string after the sub string "FM", use explode with delimiter as FM
. Do like
$uid = "855FM22";
$split = explode("FM",$uid);
var_dump($split[1]);
回答2:
You can use the explode() method.
<?php
$UID = "855FM21";
$stringParts = explode("FM", $UID);
$firstPart = $stringParts[0]; // 855
$secondPart = $stringParts[1]; // 21
?>
回答3:
use explode function it returns array. to get the last index use echo $array[count($array) - 1];
<?php
$str = "123FM23";
$array = explode("FM",$str);
echo $array[count($array) - 1];
?>
回答4:
Have you tried the explode function of php?
http://php.net/manual/en/function.explode.php
回答5:
For it,please use the explode function of php.
$UID = "855FM21";
$splitToArray = explode("FM",$UID);
print_r($splitToArray[1]);
回答6:
$uid = '123FM456';
$ArrUid = split( $uid, 'FM' );
if( count( $ArrUid ) > 1 ){
//is_numeric check ?!
$lastNumbers = $ArrUid[1];
}
else{
//no more numbers after FM
}
You can also use regular expressions to extract the last numbers!
a simple example
$uid = '1234FM56';
preg_match( '/[0-9]+fm([0-9]+)/i', $uid, $arr );
print_r($arr); //the number is on index 1 in $arr -> $arr[1]
回答7:
As a matter of best practice, never ask for more from your mysql query than you actually intend to use. The act of splitting the uid
can be done in the query itself -- and that's were I'd probably do it.
SELECT SUBSTRING_INDEX(uid, 'FM', -1) AS last_number FROM `your_tablename`
来源:https://stackoverflow.com/questions/26274259/php-splitting-a-string-after-a-specific-string