Isolate substring at end of string after a specific substring
Asked Answered
G

7

6

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"?

Garrard answered 9/10, 2014 at 8:55 Comment(0)
F
19

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]);
Forb answered 9/10, 2014 at 8:58 Comment(0)
C
3

You can use the explode() method.

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

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

?>
Cardigan answered 9/10, 2014 at 9:3 Comment(0)
H
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];
    ?>
Herra answered 9/10, 2014 at 9:3 Comment(1)
This solution may be a better answer since it gets the last segment and does not assume the needle exists only once.Irresolute
I
3

For it,please use the explode function of php.

$UID = "855FM21";
$splitToArray = explode("FM",$UID);
print_r($splitToArray[1]);
Insalivate answered 9/10, 2014 at 9:12 Comment(1)
This exact advice was already posted 14 minutes earlier and exists in the accepted answer. This answer adds no new value to the page.Wheedle
C
2

Have you tried the explode function of php?

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

Conciliatory answered 9/10, 2014 at 8:59 Comment(1)
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.Wheedle
W
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
Wheedle answered 10/12, 2018 at 2:35 Comment(0)
A
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]
Arachnoid answered 9/10, 2014 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.