热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php从第0个位置替换第一次出现的字符串-phpreplacefirstoccurrenceofstringfrom0thposition

Iwanttosearchandreplacethefirstwordwithanotherinphplikeasfollows:我想在php中搜索并替换第一个单词,如

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));
}

#7


-3  

This function str_replace is the one you are looking for.

这个函数str_replace是你要找的那个。

#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.'
';

推荐阅读
  • angular.element使用方法及总结
    2019独角兽企业重金招聘Python工程师标准在线查询:http:each.sinaapp.comangularapielement.html使用方法 ... [详细]
  • ①页面初始化----------收到客户端的请求,产生相应页面的Page对象,通过Page_Init事件进行page对象及其控件的初始化.②加载视图状态-------ViewSta ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
  • Summarize function is doing alignment without timezone ?
    Hi.Imtryingtogetsummarizefrom00:00otfirstdayofthismonthametric, ... [详细]
  • 【爬虫】关于企业信用信息公示系统加速乐最新反爬虫机制
    ( ̄▽ ̄)~又得半夜修仙了,作为一个爬虫小白,花了3天时间写好的程序,才跑了一个月目标网站就更新了,是有点悲催,还是要只有一天的时间重构。升级后网站的层次结构并没有太多变化,表面上 ... [详细]
  • fileuploadJS@sectionscripts{<scriptsrc~Contentjsfileuploadvendorjquery.ui.widget.js ... [详细]
author-avatar
手机用户2602919091
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有