简体   繁体   中英

How to convert <br> tag to <hr> tag with PHP?

I have some variable like this;

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

This variable prints;

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

source code is here:

and look like this

Now, i want to convert br tag to hr like this

在此处输入图片说明

I tried the following nospor 's codes but not worked.

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

How can i change that br tags to hr tag with PHP?

<br>Some text 1</br> - wow.... who wrote that?

Anyway... do it as simple as possible ;)

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

edit:

Probably instead of the_field you should use get_field because first one displays it and second returns.

<br> </br> is incorrect.

HTML <br> is an empty tag,so there is no end tag.

XHTML <br />

then you can use function str_replace to relace "br" by "hr"

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

Above answer is exactly correct.

You should use str_replace

the different between str_replace and 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;

the output will be:

str_replace: bar fighters, preg_replace: bar barhters
$string= the_field('reviews');
$change = str_replace(
  array('<br>', '</br>'), 
  array('<hr>', ''), 
  $string
);
echo $change;

Use simple php str_replace function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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