2015-06-08 07:40
浏览 69

PHP str_replace不能给我想要的输出

I want to replace a string at a particular position. For that I used str_replace() PHP function, but after that, I can't get an output. Here I show you what I want.

$str = "hello 8-7-2015 world -12";
// here I want replace - with ' desh ' but in date only. That I have detected using check before character if space than it should be 'minus' otherwise it should be 'desh'.

$key = strpos($str, "-");
if($key !== false){
     $a = substr($str, $key-1 , 1);
     if($a != " "){
           $str = str_replace("-","desh",$str);
           $str = str_replace("-","minus",$str);

I get output like: hello 8 desh 7 desh 2015 world desh 12 . Everywhere there is desh I want minus 12. Other values are okay and should not be changed. Means particular position change.

图片转代码服务由CSDN问答提供 功能建议

我想替换特定位置的字符串。 为此,我使用 str_replace() PHP函数,但在那之后,我无法获得输出。 在这里,我向您展示我想要的东西。

  $ str =“hello 8-7-2015 world -12”; 
 //在这里我要替换 - 用'desh' 但仅限日期。 我已经检测到在字符之前检查使用空格而不是它应该是'减'否则它应该是'desh'。
 $ key = strpos($ str,“ - ”); 
if($ key!==  false){
 $ a = substr($ str,$ key-1,1); 
 if($ a!=“”){
 $ str = str_replace(“ - ”,“desh”,$ str  ); 
} else {
 $ str = str_replace(“ - ”,“minus”,$ str); 

I 得到如下输出: hello 8 desh 7 desh 2015 world desh 12 。 到处都有 desh 我希望减去12 。 其他值是可以的,不应该更改。 特定位置更改。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • drhzc64482 2015-06-08 07:52

    Your code (with an if) doesn't loop over the string looking for all occurrences, so that should have raised an alert flag with you when all the occurrences were changed.

    What it does is to find the first occurrence, which isn't preceded by a space, then it executes:


    which replaces all occurrences within the string. In order to do what you want, all you need is:

    str_replace(" -"," minus",$str);

    This will first take care of all - character preceded by a space, turning them into " minus".

    The second line will then take care of all the remaining - characters, replacing them with "desh".

    Just as an aside, if you're doing this to be able to "speak" the words (in the sense of a text-to-speech (TTS) program), you probably want spaces on either sides of the words you're adding. You can achieve that with a very small modification:

    str_replace(" -"," minus ",$str);
    str_replace("-"," desh ",$str);

    That may make it easier for your TTS code to handle the words.

    点赞 打赏 评论
  • dtqie02844 2015-06-08 07:50

    There's no point in your condition since str_replace takes effect on whole the string without any relation to your $key variable.

           $str = str_replace(" -","minus",$str);
           $str = str_replace("-","desh",$str);

    Truth is that you don't even need that condition. Simply use the first str_replace when the search term has blank space prior to it and the second str_replce doesn't. (order it's important).

    点赞 打赏 评论
  • dongqian1893 2015-06-08 07:52

    You can use regex:

    $str = preg_replace(
        ['$1 desh $2 desh $3',             'minus $1'],
    点赞 打赏 评论
  • dongpian6319 2015-06-08 07:54

    check this, First you have to get the date from the string, then change the date format as you want after that concatenate with other strings

    $str = explode(' ',$str);
    $str1 = str_replace("-","desh",$str[1]);
    $str2 = str_replace("-","minus",$str[2]);
    $str = $str[0].$str1.$str2;
    点赞 打赏 评论

相关推荐 更多相似问题