5

My query generates a result set of UID values which looks like:

855FM21
855FM22
etc

I want to isolate the last number from the UID which it can be done by splitting the string.

How to split this string after the substring "FM"?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Akhil P M
  • 174
  • 1
  • 2
  • 13

7 Answers7

18

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]);
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
3

You can use the explode() method.

<?php
$UID = "855FM21";
$stringParts = explode("FM", $UID);

$firstPart  = $stringParts[0]; // 855
$secondPart = $stringParts[1]; // 21

?>
hfrahmann
  • 83
  • 6
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];
    ?>
waelhe
  • 381
  • 2
  • 13
  • 1
    This solution may be a better answer since it gets the last segment and does not assume the needle exists only once. – Larry Ball Aug 05 '21 at 16:22
3

For it,please use the explode function of php.

$UID = "855FM21";
$splitToArray = explode("FM",$UID);
print_r($splitToArray[1]);
Manishdlbr
  • 92
  • 7
  • This exact advice was already posted 14 minutes earlier and exists in the accepted answer. This answer adds no new value to the page. – mickmackusa Jan 13 '21 at 14:16
2

Have you tried the explode function of php?

http://php.net/manual/en/function.explode.php

Timelimelim
  • 176
  • 1
  • 11
  • This manual reference could have been provided as a comment under the question. Stack Overflow does not want to be a traffic router, it wants specific answers to the posted question to be hardcoded into answers on this page. – mickmackusa Jan 13 '21 at 14:18
2

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 where I'd probably do it.

SELECT SUBSTRING_INDEX(uid, 'FM', -1) AS last_number FROM `your_tablename`

If you need to explode, then be practice would indicate that the third parameter of explode() should set to 2. This way, the function doesn't waste any extra effort looking for more occurrences of FM.

echo explode('FM', $uid, 2)[1];  // 21

If you want to use php to isolate the trailing number in the uid, but don't want explode() for some reason, here are some wackier / less efficient techniques:

$uid = '855FM21';
echo strtok($uid, 'FM') ? strtok('FM') : '';  // 21

echo preg_replace('~.*FM~', '', $uid);  // 21

echo ltrim(ltrim($uid, '0..9'), 'MF');  // 21
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0
$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]
raiserle
  • 677
  • 8
  • 31