用PHP删除字符串的第一个实例
如何使用PHP删除另一个字符串中的子字符串的第一个实例?谢谢。如果你的正则表达式技能能够胜任的话 使用preg_replace,限值为1用PHP删除字符串的第一个实例,php,Php,如何使用PHP删除另一个字符串中的子字符串的第一个实例?谢谢。如果你的正则表达式技能能够胜任的话 使用preg_replace,限值为1 mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) 如果你的正则表达式技能达到要求 使用preg_replace,限值为1 mixed preg_replace ( mixed
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
如果你的正则表达式技能达到要求 使用preg_replace,限值为1
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
试着用这个大概的想法做点什么:
$search = "boo";
$str = "testtestbootest";
$pos = strpos(strrev($str), strrev($search));
$newstr = substr($str, 0, $pos) . substr($str, $pos + strlen($search));
试着用这个大概的想法做点什么:
$search = "boo";
$str = "testtestbootest";
$pos = strpos(strrev($str), strrev($search));
$newstr = substr($str, 0, $pos) . substr($str, $pos + strlen($search));
像这样的事情可能会奏效
function replaceFirst($input, $search, $replacement){
$pos = stripos($input, $search);
if($pos === false){
return $input;
}
else{
$result = substr_replace($input, $replacement, $pos, strlen($search));
return $result;
}
}
$input = "This is a test. This is only a test.";
$search = "test";
echo replaceFirst($input, $search, "replaced!");
// "This is a replaced!. This is only a test."
很抱歉进行了所有编辑,出现了一些奇怪的格式问题。像这样的东西可能会起作用
function replaceFirst($input, $search, $replacement){
$pos = stripos($input, $search);
if($pos === false){
return $input;
}
else{
$result = substr_replace($input, $replacement, $pos, strlen($search));
return $result;
}
}
$input = "This is a test. This is only a test.";
$search = "test";
echo replaceFirst($input, $search, "replaced!");
// "This is a replaced!. This is only a test."
很抱歉进行了所有编辑,出现了一些奇怪的格式问题。这是一个副本,请参阅:此处提供了解决方案。这是一个副本,请参阅:此处提供了解决方案。