开发者

How to get position of last letter in a string in php?

开发者 https://www.devze.com 2023-01-01 00:44 出处:网络
Example of a string: \"Some text.....!!!!!!开发者_开发技巧!!?????\" Using PHP how would I get the position of the last letter (or even alphanum character) which in this example is the letter t?You co

Example of a string: "Some text.....!!!!!!开发者_开发技巧!!?????"

Using PHP how would I get the position of the last letter (or even alphanum character) which in this example is the letter t?


You could use preg_match_all with the regular expression \p{L} to find all Unicode letters. With the additional flag PREG_OFFSET_CAPTURE you get also the offsets:

$str = "Some text.....!!!!!!!!?????";
if (preg_match_all('/\p{L}/u', $str, $matches, PREG_OFFSET_CAPTURE) > 0) {
    $lastMatch = $matches[0][count($matches[0])-1];
    echo 'last letter: '.$lastMatch[0].' at '.$lastMatch[1];
} else {
    echo 'no letters found';
}


$s = "Some text.....!!!!!!!!embedded?????";

$words = str_word_count($s,2);
$lastLetterPos = array_pop(array_keys($words)) + strlen(array_pop($words)) - 1;
echo $lastLetterPos;

If you want to allow for alphanum rather than just alpha:

$s = "Some text.....!!!!!!!!embedded21?????";

$words = str_word_count($s,2,'0123456789');
$lastLetterPos = array_pop(array_keys($words)) + strlen(array_pop($words)) - 1;
echo $lastLetterPos;

To add other characters as valid:

$s = "Some text.....!!!!!!!!embedded!!à?????"; 

$words = str_word_count($s,2,'0123456789ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ'); 
$lastLetterPos = array_pop(array_keys($words)) + strlen(array_pop($words)) - 1; 
echo $lastLetterPos; 


try substr() ;)

echo substr('abcdef', -1, 1);


It all depends on what you consider a letter, and what you want to do with "Some!!!???text???!!!" for example:

The naive solution would be to iterate from the last position in the string to find the first character you want to keep, then return that. (Or iterate from the beginning to find the first character you want to stop at, then return the one before it.)

Or you can use a regex to match what you want to keep, then take the last match. (Or use a regex replace to remove what you don't want to keep, then take the last character.)


Naive:

<?php
    $s = "Some text.....!!!!!!!!?????";
    $ary = str_split($s);
    $position = 0;
    $last = 0;
    foreach ($ary as $char) {
        if (preg_match("/[a-zA-Z0-9]/", $char)) {
            $last = $position;
        }
        $position += 1;
    }
    echo $last;
?>


<?php

$str    = '"Some text.....!!!!!!!!?????"';
$last   = -1;

foreach(range('a', 'z') as $letter)
{
    $last = max($last, strripos($str, $letter));
}

echo "$last\n"; // 9

?>


Here is a simple and straight-forward O(n) algorithm.

From the manual for ctype_alpha():

In the standard C locale letters are just [A-Za-z]

If you need a different locale, you should abstract the function that determines if a character is alpha or not. That way you can keep this algorithm and adapt to varying languages.

function lastAlphaPosition($string) {
    $lastIndex = strlen($string)-1;
    for ($i = $lastIndex; $i > 0; --$i) {
        if (ctype_alpha($string[$i])) {
            return $i;
        }
    }
    return -1;
}

$testString = 'Some text.....!!!!!!!!?????';
$lastAlphaPos = lastAlphaPosition($testString);

if ($lastAlphaPos !== -1) {
    echo $testString[$lastAlphaPos];
} else {
    echo 'No alpha characters found.';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号