drgzmmy6379 2012-05-24 06:09
浏览 6
已采纳

如何只查找一次查找字符串?

For example there is a string :

$valeur = "a-b-c-b-d-e";

The letter "b" is present twice in this. I want to replace only the first "b".

How to do that ? I used str_replace but it replaces all occurences.

  • 写回答

2条回答 默认 最新

  • duanhuiuw12555 2012-05-24 06:17
    关注

    You cab try preg_replace here.

        $valeur = "a-b-c-b-d-e";
        echo preg_replace('/b/', 'x', $valeur, 1); // outputs 'a-x-c-b-d-e'
    

    Here 4th parameter is for limit and this is optional.

    thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?