将字符串分成两个单词后,将两个字母大写

时间:2019-03-01 07:58:12

标签: php

我有一个字符串:

$name = ucfirst('kasiang');

我想将其拆分为两个单词并创建Ka Siang

所以我尝试了:

echo substr_replace($name,' '.substr($name,2),2)

但是我只得到Ka siang。还有什么办法可以大写第二个字母吗?

2 个答案:

答案 0 :(得分:2)

您可以按照以下方式进行操作。

<?php 

$name = ucfirst('kasiang');
echo substr_replace($name,' '.ucfirst(substr($name,2)),2);

?>

另一种方法是:

<?php 
echo ucwords(substr_replace($name,' '.substr($name,2),2));
?>

答案 1 :(得分:2)

您可以使用ucwords进行操作,如上面的评论所述:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/multiple-select/1.2.2/multiple-select.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/multiple-select/1.2.2/multiple-select.min.css" rel="stylesheet" />
<div class="container">
  <div class="form-group mt-5">
    <select multiple class="" id="exampleFormControlSelect2">
      <option value="pugal" class="options">Pugal</option>
      <option value="jino" class="options">Jino John</option>
      <optgroup label="Group 1" class="group-title">
        <option value="1" class="sub-options">Pk Prabu</option>
        <option value="2" class="sub-options">Gora Ramamoorthy</option>
      </optgroup>
      <optgroup label="Group 2" class="group-title">
        <option value="11" class="sub-options">210</option>
        <option value="12" class="sub-options">321</option>
      </optgroup>
      <optgroup label="Group 3" class="group-title">
        <option value="20" class="sub-options">012</option>
        <option value="21" class="sub-options">123</option>
      </optgroup>
    </select>
  </div>
</div>

结果:

<?php
$name = ucfirst('kasiang');

echo ucwords(substr_replace($name,' '.substr($name,2),2));