Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 只替换一个重复的单词_Php_Replace - Fatal编程技术网

Php 只替换一个重复的单词

Php 只替换一个重复的单词,php,replace,Php,Replace,即使一个单词重复了很多次,一个句子中是否可以只替换一个单词: <?php $sentence 'Hello my name is Abu Rayane, my age is 99 yearsl old, and my school level is -1'; $new_sentence = str_replace('my', '__', $sentence); echo $new_sentence; ?> 我只想替换第一个“我的” 提前感谢使用此:,带有$limit参数。Ale

即使一个单词重复了很多次,一个句子中是否可以只替换一个单词:

<?php
$sentence 'Hello my name is Abu Rayane, my age is 99 yearsl old, and my school level is -1';
$new_sentence = str_replace('my', '__', $sentence);
echo $new_sentence;
?>

我只想替换第一个“我的”

提前感谢

使用此:,带有$limit参数。

AlexDeb是正确的

以下是一个例子:

<?php
$sentence = 'Hello my name is Abu Rayane, my age is 99 yearsl old, and my school level is -1';
$new_sentence = preg_replace('/my/', '__', $sentence, 1);
echo $new_sentence;
?>


有答案,请仔细阅读:-)我找不到任何答案是的,如果我输入2,输出是:Hello uuuuu名字是Abu Rayane,uuuuuuuu年龄是99岁,我的学校级别是-1我使用了下面的示例,但它在输入分隔符2的同时替换了我的第一和第二