简体   繁体   English

更换 <br> 与 <n> 在PHP中的数组

[英]To replace the <br> with the <n> in php in an array

<?php
  $servername="localhost";
  $username="root";
  $password="";
  $dbname="abc";
  $sql=mysqli_connect($servername,$username,$password,$dbname)
  $get=mysqli_query($sql,"SELECT * FORM table_name")

  $dta=array();
  while($fetch=mysqli_fetch_assoc($get)){
      array_push($dta,$fetch);
  }

  $dta=preg_replace("/<br>/",'\n',$dta);
  $json=json_encode($dta);
  echo $json;
  exit;
?>

I want to replace the data form the data base. 我想从数据库中替换数据。 Want to replace column values from 要替换列中的值
with

For example: 例如:

  1. I have some columns like A, B, C 我有一些列,例如A,B,C
  2. Column A has data like qwe <br> qu 列A具有类似qwe <br> qu数据
  3. I want the result of column A would be like qwe <n> qu 我希望列A的结果像qwe <n> qu

Note: The above small example has more than 20 columns. 注意:上面的小示例有20多个列。

Change: 更改:

$dta=preg_replace("/<br>/",'\n',$dta);

To: 至:

array_walk($dta, function(&$val){
    $val = str_replace("<br>", "\r\n", $val);
});

& keep in mind: 并记住:

  • For \\n use double quotes " instead of single quotes ' . 对于\\n使用双引号"而不是单引号'
  • If the resulted string having new lines will be used in Windows, replace with \\r\\n instead of just \\n 如果在Windows中将使用带有换行符的结果字符串,请替换为\\r\\n而不是\\n
  • There is no need for preg_replace , str_replace would perfectly fit 不需要preg_replacestr_replace将完全适合
  • As $dta is an array, we have to use array_walk or loop in the array. 由于$dta是一个数组,因此我们必须在数组中使用array_walk或loop。

Your code has so many bugs. 您的代码有很多错误。

  1. $fetch holds an array (not a string) $fetch保存一个数组(不是字符串)
  2. $dta is then an array of arrays. 那么$dta是一个数组数组。
  3. preg_replace works only on strings not arrays. preg_replace仅适用于字符串,不适用于数组。

You have first to fix that issue. 您必须首先解决该问题。

Replace this line 替换此行

array_push($dta,$fetch);

with this: 有了这个:

//update all columns from the current fetched row
$fetch = array_map(function($a){ 
       return str_replace(['<br/>','<br>','<br />'],"\n",$a);
},$fetch);
//add the data
array_push($dta,$fetch);

and remove the preg_match line. 并删除preg_match行。

What does ['<br/>','<br>','<br />'] in str_replace? str_replace中的['<br/>','<br>','<br />']是什么?

If you have an array as first parameter then each entry will searched an replaced. 如果将数组作为第一个参数,则每个条目都将搜索一个替换项。 In this example we dont really know how <br> is written in the code. 在此示例中,我们真的不知道<br>是如何在代码中编写的。 Can be <br/> <br> <br /> , so i have added all the cases here. 可以是<br/> <br> <br /> ,所以我在这里添加了所有情况。

More infos: 更多信息:

http://php.net/manual/en/function.str-replace.php use of str_replace http://php.net/manual/zh/function.str-replace.php使用str_replace

http://php.net/manual/en/function.array-map.php use of array_map 使用array_map的http://php.net/manual/zh-CN/function.array-map.php

http://php.net/manual/en/functions.anonymous.php use of function(){} http://php.net/manual/zh/functions.anonymous.php function(){}

https://stackoverflow.com/a/10304027/4916265 more about function()use(){} https://stackoverflow.com/a/10304027/4916265有关function()use(){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM