PHP函数将字符串中的每个字符小写,除了最后一个字符

时间:2018-04-25 10:26:32

标签: php

我正在尝试小写字符串中的每个字符,除了最后一个应该是大写的字符。

这是我的代码:

<?xml version="1.0"?>
<VTKFile type="PStructuredGrid" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
  <PStructuredGrid WholeExtent="0 20 0 10 0 10" GhostLevel="2">
    <PCellData>
      <PDataArray type="Float32" Name="density"/>
    </PCellData>
    <PPoints>
      <PDataArray type="Float32" Name="Points" NumberOfComponents="3"/>
    </PPoints>
    <Piece Extent="0 10 0 10 0 10" Source="test_0.vts"/>
    <Piece Extent="10 20 0 10 0 10" Source="test_1.vts"/>
  </PStructuredGrid>
</VTKFile>

5 个答案:

答案 0 :(得分:2)

这是此问题的简单解决方案

function caps_caps($var) {
    $var = strrev(ucwords(strrev(strtolower($var))));
    echo $var;
}
caps_caps("HeLlo WOrld");

Demo

答案 1 :(得分:1)

function caps_caps($text) {
    $value_to_print = '';
    $text = strrev(ucwords(strrev($text)));
    $words = explode(' ', $text);
    foreach($words as $word){
        $word = strtolower($word);
        $word[strlen($word)-1] = strtoupper($word[strlen($word)-1]);
        $value_to_print .= $word . ' ';
    }
    echo trim($value_to_print);
}

caps_caps("HeLlo WOrld");

答案 2 :(得分:1)

您还需要先将字符串转换为小写。

function caps_caps($var) {
    $var = strrev(ucwords(strrev(strtolower($var))));
    echo $var;
}

caps_caps("HeLlo WOrld"); // returns "hellO worlD"

答案 3 :(得分:1)

试试这个,你忘了做每个元素。

function uclast_words($text, $delimiter = " "){

    foreach(explode($delimiter, $text) as $value){
        $temp[] = strrev(ucfirst(strrev(strtolower($value))));
    }

    return implode($delimiter, $temp);
}

print_r(uclast_words("hello world", " "));

我希望这是你问题的答案。

答案 4 :(得分:1)

You can try this piece of code.
function uclast($s)
{
  $lastCharacterUppar = '';
  if ( preg_match('/\s/',$s) ){//If string has space
      $explode = explode(' ',$s);
      for($i=0;$i<count($explode);$i++){
        $l=strlen($explode[$i])-1;
        $explode[$i] = strtolower($explode[$i]);
        $explode[$i][$l] = strtoupper($explode[$i][$l]);    
      }
      $lastCharacterUppar = implode(' ', $explode);
  } else { //if string without space
    $l=strlen($s)-1;
    $s = strtolower($s);
    $s[$l] = strtoupper($s[$l]);
    $lastCharacterUppar = $s;    
  }
  return $lastCharacterUppar;
}

$str = 'hey you yo';
echo uclast($str);