简体   繁体   中英

How to get position of my letter from string using PHP?

The letter is m and my string is asdvbgmfger .

When I run the code I want to get m position is 6 . The position start at 0 .

I use this code but didn't work:

<?php
$str = "asdvbgmfger";

if (preg_match_all('/\p{L}/u', $str, $matches, PREG_OFFSET_CAPTURE) > 0) {
    $lastMatch = "m";
    echo 'm position is '.$lastMatch[1];
} else {
    echo 'no letters found';
}
?>

have you tried this way: https://3v4l.org/l0p5a

<?php
$str = "asdvbgmfger";
$strPosition = strpos($str, 'm');

if ($strPosition !== false) {
    echo 'm position is ' . $strPosition;
} else {
    echo 'no letters found';
}

hope it helps.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM