Get the last word of a string

后端 未结 7 2288
时光说笑
时光说笑 2020-12-15 04:10

I have tried a few things to get a last part out I done this:

$string = \'Sim-only 500 | Internet 2500\';
preg_replace(\"Sim-Only ^([1-9]|[1-9][0-9]|[1-9][0-         


        
相关标签:
7条回答
  • 2020-12-15 05:16

    There you go a generic function to get last words from string

    public function get_last_words($amount, $string)
    {
        $amount+=1;
        $string_array = explode(' ', $string);
        $totalwords= str_word_count($string, 1, 'àáãç3');
        if($totalwords > $amount){
            $words= implode(' ',array_slice($string_array, count($string_array) - $amount));
        }else{
            $words= implode(' ',array_slice($string_array, count($string_array) - $totalwords));
        }
    
        return $words;
    }
    $string = '​Sim-​only 500 | Internet 2500​';
    echo get_last_words(1,  $string );
    
    0 讨论(0)
提交回复
热议问题