[英]How to convert <br> tag to <hr> tag with PHP?
我有一些这样的变量;
<?php the_field('reviews'); ?>
该变量打印;
Some text 1
Some text 2
Some text 3
Some text 4
源代码在这里:
看起来像这样
现在,我想像这样将br标签转换为hr
我尝试了以下nospor的代码,但是没有用。
$change = the_field('reviews');
$change = str_replace(array('<br>', '</br>'), array('', '<hr>'), $change);
echo $change;
如何使用PHP将br标签更改为hr标签?
<br>Some text 1</br>
-哇...。谁写的?
无论如何...尽可能简单;)
$change = the_field('reviews');
$change = str_replace(array('<br>', '</br>'), array('', '<hr>'), $change);
echo $change;
编辑:
也许代替the_field
你应该使用get_field
因为第一个显示它和第二的回报。
<br> </br>
不正确。
HTML <br>
是一个空标签,因此没有结束标签。
XHTML <br />
那么您可以使用str_replace
函数将“ br”减为“ hr”
$tag_br = array(
"<br>",
"</br>"
);
$tag_hr = array(
"<hr>",
""
);
$change = str_replace($tag_br,$tag_hr ,$change);
以上答案完全正确。
您应该使用str_replace
str_replace
和preg_replace
之间的区别
$string = "foo fighters";
$str_replace = str_replace('foo','bar',$string);
$preg_replace = preg_replace('/f.{2}/','bar',$string);
echo 'str_replace: ' . $str_replace . ', preg_replace: ' . $preg_replace;
输出将是:
str_replace: bar fighters, preg_replace: bar barhters
$string= the_field('reviews');
$change = str_replace(
array('<br>', '</br>'),
array('<hr>', ''),
$string
);
echo $change;
使用简单的PHP str_replace函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.