• <bdo id='YVeXB'></bdo><ul id='YVeXB'></ul>

      <small id='YVeXB'></small><noframes id='YVeXB'>

      <legend id='YVeXB'><style id='YVeXB'><dir id='YVeXB'><q id='YVeXB'></q></dir></style></legend>
      <tfoot id='YVeXB'></tfoot>

        <i id='YVeXB'><tr id='YVeXB'><dt id='YVeXB'><q id='YVeXB'><span id='YVeXB'><b id='YVeXB'><form id='YVeXB'><ins id='YVeXB'></ins><ul id='YVeXB'></ul><sub id='YVeXB'></sub></form><legend id='YVeXB'></legend><bdo id='YVeXB'><pre id='YVeXB'><center id='YVeXB'></center></pre></bdo></b><th id='YVeXB'></th></span></q></dt></tr></i><div id='YVeXB'><tfoot id='YVeXB'></tfoot><dl id='YVeXB'><fieldset id='YVeXB'></fieldset></dl></div>

        用数组中的值替换所有出现的字符串

        Replacing all occurrences of a string with values from an array(用数组中的值替换所有出现的字符串)
      1. <tfoot id='DBCKP'></tfoot>
          <legend id='DBCKP'><style id='DBCKP'><dir id='DBCKP'><q id='DBCKP'></q></dir></style></legend>
            • <bdo id='DBCKP'></bdo><ul id='DBCKP'></ul>

                <tbody id='DBCKP'></tbody>
                • <small id='DBCKP'></small><noframes id='DBCKP'>

                  <i id='DBCKP'><tr id='DBCKP'><dt id='DBCKP'><q id='DBCKP'><span id='DBCKP'><b id='DBCKP'><form id='DBCKP'><ins id='DBCKP'></ins><ul id='DBCKP'></ul><sub id='DBCKP'></sub></form><legend id='DBCKP'></legend><bdo id='DBCKP'><pre id='DBCKP'><center id='DBCKP'></center></pre></bdo></b><th id='DBCKP'></th></span></q></dt></tr></i><div id='DBCKP'><tfoot id='DBCKP'></tfoot><dl id='DBCKP'><fieldset id='DBCKP'></fieldset></dl></div>
                  本文介绍了用数组中的值替换所有出现的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在将字符串发送到数据库之前对其进行解析.我想查看该字符串中的所有 <br> 并将它们替换为我从数组中获得的唯一数字,后跟一个 newLine.

                  I am parsing a string before sending it to a DB. I want to go over all <br> in that string and replace them with unique numbers that I get from an array followed by a newLine.

                  例如:

                  str = "Line <br> Line <br> Line <br> Line <br>"
                  $replace = array("1", "2", "3", "4");
                  
                  my function would return 
                  "Line 1 
                   Line 2 
                   Line 3 
                   Line 4 
                  "
                  

                  听起来很简单.我只会做一个while循环,使用strpos获取<br>的所有出现,并使用str_replace将它们替换为所需的数字+ .

                  Sounds simple enough. I would just do a while loop, get all the occurances of <br> using strpos, and replace those with the required numbers+ using str_replace.

                  问题是我总是出错,我不知道我做错了什么?可能是一个愚蠢的错误,但仍然很烦人.

                  Problem is that I always get an error and I have no idea what I am doing wrong? Probably a dumb mistake, but still annoying.

                  这是我的代码

                  $str     = "Line <br> Line <br> Line <br> Line <br>";
                  $replace = array("1", "2", "3", "4");
                  $replaceIndex = 0;
                  
                  while(strpos($str, '<br>') != false )
                  {
                      $str = str_replace('<br>', $replace[index] . ' ' .'
                  ', $str); //str_replace, replaces the first occurance of <br> it finds
                      index++;
                  }
                  

                  有什么想法吗?

                  提前致谢,

                  推荐答案

                  我会使用正则表达式和自定义回调,如下所示:

                  I would use a regex and a custom callback, like this:

                  $str = "Line <br> Line <br> Line <br> Line <br>";
                  $replace = array("1", "2", "3", "4");
                  $str = preg_replace_callback( '/<br>/', function( $match) use( &$replace) {
                      return array_shift( $replace) . ' ' . "
                  ";
                  }, $str);
                  

                  请注意,这里假设我们可以修改 $replace 数组.如果不是这种情况,您可以保留一个计数器:

                  Note that this assumes we can modify the $replace array. If that's not the case, you can keep a counter:

                  $str = "Line <br> Line <br> Line <br> Line <br>";
                  $replace = array("1", "2", "3", "4");
                  $count = 0;
                  $str = preg_replace_callback( '/<br>/', function( $match) use( $replace, &$count) {
                      return $replace[$count++] . ' ' . "
                  ";
                  }, $str);
                  

                  你可以从这个演示看到它输出:

                  Line 1 Line 2 Line 3 Line 4 
                  

                  这篇关于用数组中的值替换所有出现的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tbody id='6LsCL'></tbody>

                  <small id='6LsCL'></small><noframes id='6LsCL'>

                  <i id='6LsCL'><tr id='6LsCL'><dt id='6LsCL'><q id='6LsCL'><span id='6LsCL'><b id='6LsCL'><form id='6LsCL'><ins id='6LsCL'></ins><ul id='6LsCL'></ul><sub id='6LsCL'></sub></form><legend id='6LsCL'></legend><bdo id='6LsCL'><pre id='6LsCL'><center id='6LsCL'></center></pre></bdo></b><th id='6LsCL'></th></span></q></dt></tr></i><div id='6LsCL'><tfoot id='6LsCL'></tfoot><dl id='6LsCL'><fieldset id='6LsCL'></fieldset></dl></div>

                    <bdo id='6LsCL'></bdo><ul id='6LsCL'></ul>
                    <tfoot id='6LsCL'></tfoot>

                        1. <legend id='6LsCL'><style id='6LsCL'><dir id='6LsCL'><q id='6LsCL'></q></dir></style></legend>