如何用PHP将<br/>标签转换为<hr />标签?

时间:2016-06-01 08:11:50

标签: php html

我有一些像这样的变量;

<?php the_field('reviews'); ?>

此变量打印;

Some text 1
Some text 2
Some text 3
Some text 4

源代码在这里:

看起来像这样

现在,我想将 br 标记转换为 hr ,就像这样

enter image description here

我尝试了以下 nospor 的代码,但没有效果。

$change = the_field('reviews');
$change = str_replace(array('<br>', '</br>'), array('', '<hr>'), $change);
echo $change;

如何使用PHP将 br 标记更改为 hr 标记?

4 个答案:

答案 0 :(得分:1)

<form action="upload_photo.php?id=<?php echo $row1['id']?>"> - 哇....是谁写的?

无论如何......尽可能简单;)

<br>Some text 1</br>

编辑:

可能使用$change = the_field('reviews'); $change = str_replace(array('<br>', '</br>'), array('', '<hr>'), $change); echo $change; 代替the_field,因为第一个显示它,第二个返回。

答案 1 :(得分:1)

<br> </br>不正确。

HTML <br>是一个空标记,因此没有结束标记。

XHTML <br />

然后你可以使用函数str_replace来重新发布&#34; br&#34;通过&#34; hr&#34;

$tag_br = array(
  "<br>",
  "</br>"
);
$tag_hr = array(
  "<hr>",
  ""
);
$change = str_replace($tag_br,$tag_hr ,$change);

答案 2 :(得分:0)

Above answer完全正确。

您应该使用str_replace

str_replacepreg_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

答案 3 :(得分:-3)

$string= the_field('reviews');
$change = str_replace(
  array('<br>', '</br>'), 
  array('<hr>', ''), 
  $string
);
echo $change;

使用简单的php str_replace函数

相关问题