php php从第0个位置替换第一次出现的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9598665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php replace first occurrence of string from 0th position
提问by Ben
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
通过搜索将“无”替换为“有”,并在不使用的情况下替换 substr
output should be: 'something inside'
输出应该是:'里面的东西'
回答by Milind Ganjoo
Use preg_replace()
with a limit of 1:
使用preg_replace()
限制为 1:
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/
您要搜索的任何字符串替换正则表达式。由于正则表达式总是从左到右计算,这将始终匹配第一个实例。
回答by mishu
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
回答by Uday Sawant
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 。注意replaceWord 后面的一个空格。这是因为我们添加\s
了搜索字符串
回答by trex005
preg_replace('/nothing/', 'something', $str, 1);
回答by Jayo2k
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
,撇号会导致问题。所以我想出了一个小技巧:
$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;
}
回答by Luca C.
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));
}
回答by steveoh
This function str_replace
is the one you are looking for.
此功能str_replace
正是您正在寻找的功能。
回答by stickmanusa
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>';