0

基本的に、文字列がすべて大文字であるかどうかを検出し、それに応じて文字列を大文字にするかどうかを置き換える簡単な方法があるかどうか疑問に思っています。

例:

 $str = "WELL HI THERE BOB!";
 $str = preg_replace("hi", "greetings", $str);

それだと「WELL greetings THERE BOB」って出ちゃうけど、「WELL GREETINGS THERE BOB」って出てほしい。

そして、私は欲しい:

 $str = "Well hi there bob";
 $str = preg_replace("hi", "greetings", $str);

カミングアウトするには:「こんにちはボブ!」

繰り返しますが、すべて大文字のままにしておきたいのですが、すでにすべて大文字になっている場合に限ります。

4

3 に答える 3

0

これを試して:

<?php $str = "WELL HI THERE BOB!";  
 $str = str_replace('hi', 'greetings', $str);
 $str = str_replace('HI', 'GREETINGS', $str);
    if (!(strtoupper($str) == $str)) {      
    $str = ucfirst(strtolower($str)); 
  } else{
    $str = strtoupper($str);      
  }
 echo $str; 
 ?>

出力:

入力が の場合:$str = "WELL HI THERE BOB!"

WELL GREETINGS THERE BOB!

および入力が $str = "WELL hi THERE BOB!";

Well greetings there bob! 
于 2013-04-30T03:51:10.720 に答える