• <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>

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

        时间:2023-10-11
      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 
                  

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

                  上一篇:xampp localhost phpmyadmin 用户 'root'@'localhost& 下一篇:#2002 - 服务器没有响应(或者本地 MySQL 服务器的套接字没有正确配置)

                  相关文章

                  <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>