dpnvrt3119 2017-04-20 10:36
浏览 86
已采纳

用数组中的php中的<n>替换<br>

<?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>/",'
',$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
  2. Column A has data like qwe <br> qu
  3. I want the result of column A would be like qwe <n> qu

Note: The above small example has more than 20 columns.

  • 写回答

2条回答 默认 最新

  • duanning9110 2017-04-20 10:58
    关注

    Your code has so many bugs.

    1. $fetch holds an array (not a string)
    2. $dta is then an array of arrays.
    3. preg_replace works only on strings not arrays.

    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 />'],"
    ",$a);
    },$fetch);
    //add the data
    array_push($dta,$fetch);
    

    and remove the preg_match line.

    What does ['<br/>','<br>','<br />'] in str_replace?

    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. Can be <br/> <br> <br />, so i have added all the cases here.

    More infos:

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

    http://php.net/manual/en/function.array-map.php use of array_map

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程