[英]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: 例如:
qwe <br> qu
列A具有类似qwe <br> qu
数据 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: 并记住:
\\n
use double quotes "
instead of single quotes '
. 对于\\n
使用双引号"
而不是单引号'
。 \\r\\n
instead of just \\n
如果在Windows中将使用带有换行符的结果字符串,请替换为\\r\\n
而不是\\n
preg_replace
, str_replace
would perfectly fit 不需要preg_replace
, str_replace
将完全适合 $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. 您的代码有很多错误。
$fetch
holds an array (not a string) $fetch
保存一个数组(不是字符串) $dta
is then an array of arrays. 那么$dta
是一个数组数组。 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.