I want to search and replace the first word with another in php like as follows:
我想在php中搜索并替换第一个单词,如下所示:
$str="nothing inside";
Replace 'nothing' to 'something' by search and replace without using substr
通过搜索将'nothing'替换为'something',并在不使用substr的情况下替换
output should be: 'something inside'
输出应该是:'里面的东西'
8 个解决方案
#1
38
Use preg_replace()
with a limit of 1:
使用限制为1的preg_replace():
preg_replace('/nothing/', 'something', $str, 1);
Replace the regular expression /nothing/
with whatever string you want to search for. Since regular expressions are always evaluated left-to-right, this will always match the first instance.
将正则表达式/ nothing /替换为您要搜索的任何字符串。由于正则表达式始终从左到右进行计算,因此始终与第一个实例匹配。
#2
11
on the man page for str_replace (http://php.net/manual/en/function.str-replace.php) you can find this function
在str_replace的手册页(http://php.net/manual/en/function.str-replace.php)上你可以找到这个函数
function str_replace_once($str_pattern, $str_replacement, $string){
if (strpos($string, $str_pattern) !== false){
$occurrence = strpos($string, $str_pattern);
return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
}
return $string;
}
usage sample: http://codepad.org/JqUspMPx
用法示例:http://codepad.org/JqUspMPx
#3
3
try this
尝试这个
preg_replace('/^[a-zA-Z]\s/', 'ReplacementWord ', $string)
what it does is select anything from start till first white space and replace it with replcementWord . notice a space after replcementWord. this is because we added \s
in search string
它的作用是从开始到第一个空格中选择任何东西并用replcementWord替换它。在replcementWord之后注意一个空格。这是因为我们在搜索字符串中添加了\ s
#4
0
preg_replace('/nothing/', 'something', $str, 1);
#5
-1
I ran to this issue and wanted a solution and this wasn't 100% right for me because if the string was like $str = "mine'this
, the appostrophe would cause a problem. so I came up with a litle trick :
我遇到了这个问题,想要一个解决方案,这对我来说并不是100%正确,因为如果字符串就像$ str =“mine'this,那么appostrophe会引起问题。所以我想出了一个小问题:
$stick='';
$cook = explode($str,$cookie,2);
foreach($cook as $c){
if(preg_match("/^'/", $c)||preg_match('/^"/', $c)){
//we have 's dsf fds... so we need to find the first |sess| because it is the delimiter'
$stick = '|sess|'.explode('|sess|',$c,2)[1];
}else{
$stick = $c;
}
$cookies.=$stick;
}
#6
-1
This checks and caches the first substring position in one command, next replacing it if present, should be the more compact and performant:
这将在一个命令中检查并缓存第一个子字符串位置,如果存在,则替换它,应该是更紧凑和高性能:
if(($offset=strpos($string,$replaced))!==false){
$string=substr_replace($replaced,$replacer,$offset,strlen($replaced));
}
#8
-3
ltrim() will remove the unwanted text at the beginning of a string.
ltrim()将删除字符串开头的不需要的文本。
$do = 'nothing'; // what you want
$dont = 'something'; // what you dont want
$str = 'something inside';
$newstr = $do.ltrim( $str , $dont);
echo $newstr.'<br>';
#1
38
Use preg_replace()
with a limit of 1:
使用限制为1的preg_replace():
preg_replace('/nothing/', 'something', $str, 1);
Replace the regular expression /nothing/
with whatever string you want to search for. Since regular expressions are always evaluated left-to-right, this will always match the first instance.
将正则表达式/ nothing /替换为您要搜索的任何字符串。由于正则表达式始终从左到右进行计算,因此始终与第一个实例匹配。
#2
11
on the man page for str_replace (http://php.net/manual/en/function.str-replace.php) you can find this function
在str_replace的手册页(http://php.net/manual/en/function.str-replace.php)上你可以找到这个函数
function str_replace_once($str_pattern, $str_replacement, $string){
if (strpos($string, $str_pattern) !== false){
$occurrence = strpos($string, $str_pattern);
return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
}
return $string;
}
usage sample: http://codepad.org/JqUspMPx
用法示例:http://codepad.org/JqUspMPx
#3
3
try this
尝试这个
preg_replace('/^[a-zA-Z]\s/', 'ReplacementWord ', $string)
what it does is select anything from start till first white space and replace it with replcementWord . notice a space after replcementWord. this is because we added \s
in search string
它的作用是从开始到第一个空格中选择任何东西并用replcementWord替换它。在replcementWord之后注意一个空格。这是因为我们在搜索字符串中添加了\ s
#4
0
preg_replace('/nothing/', 'something', $str, 1);
#5
-1
I ran to this issue and wanted a solution and this wasn't 100% right for me because if the string was like $str = "mine'this
, the appostrophe would cause a problem. so I came up with a litle trick :
我遇到了这个问题,想要一个解决方案,这对我来说并不是100%正确,因为如果字符串就像$ str =“mine'this,那么appostrophe会引起问题。所以我想出了一个小问题:
$stick='';
$cook = explode($str,$cookie,2);
foreach($cook as $c){
if(preg_match("/^'/", $c)||preg_match('/^"/', $c)){
//we have 's dsf fds... so we need to find the first |sess| because it is the delimiter'
$stick = '|sess|'.explode('|sess|',$c,2)[1];
}else{
$stick = $c;
}
$cookies.=$stick;
}
#6
-1
This checks and caches the first substring position in one command, next replacing it if present, should be the more compact and performant:
这将在一个命令中检查并缓存第一个子字符串位置,如果存在,则替换它,应该是更紧凑和高性能:
if(($offset=strpos($string,$replaced))!==false){
$string=substr_replace($replaced,$replacer,$offset,strlen($replaced));
}
#7
#8
-3
ltrim() will remove the unwanted text at the beginning of a string.
ltrim()将删除字符串开头的不需要的文本。
$do = 'nothing'; // what you want
$dont = 'something'; // what you dont want
$str = 'something inside';
$newstr = $do.ltrim( $str , $dont);
echo $newstr.'<br>';