php:替换double< br />与< p>< p> [英] php: replacing double <br /> with </p><p>

查看:88
本文介绍了php:替换double< br />与< p>< p>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nicEdit在我的CMS中编写RTF数据。问题在于它会产生如下字符串:

  hello第一行< br>< br />这是第二行line< br />这是第三行

因为这是一个新闻网站,

 < p> hello first line< / p>< p>这是第二行< br />这是第三行< / p> 

所以我现在的解决方案是这样的:


  1. 我需要在字符串的开始/结尾修剪< br /> 的$ data
  2. $ b $将< / p>< p> (允许一个< br /> )。
  3. 最后,添加

    开始处,< / p> 结尾处

到目前为止,我只有步骤1和3。可以有人给我一个第2步的手吗?

 函数replace_br($ data){
#step 1
$ data = trim($ data,'< p>);
$ data = trim($ data,'< / p>');
$ data = trim($ data,'< br />');
#步骤2
// preg_replace()?
#步骤3
$ data ='< p>'。$ data。'< / p>';
返回$ data;
}

谢谢!

ps:避免特定情况会更好。例如: hello< br />< br />< br />< br />< br />太多空间 - 这5条breaklines也应该转换为一个< / p>< p>



final solution (特别感谢kemp!)

 函数sanitize_content($ data){
$ data = strip_tags($ data,'< p> ;,,< img>,< a>,< strong>,< u>,< em> ;,< blockquote> ,<醇>,< UL>,<李>,<跨度>');
$ data = trim($ data,'< p>);
$ data = trim($ data,'< / p>');
$ data = trim($ data,'< br />');
$ data = preg_replace('#(?:< br \ s * /?> \ s *?){2,}#','< / p>< p>' $的数据);
$ data ='< p>'。$ data。'< / p>';
返回$ data;


解决方案

即使两个< br> s在不同的行上(即它们之间有换行符或任何空格):

 函数replace_br($ data){
$ data = preg_replace('#(?:< br \ s * /?> \ s *?){2 ,}#','< / p>< p>',$ data);
返回< p> $ data< / p>;
}


i use nicEdit to write RTF data in my CMS. The problem is that it generates strings like this:

hello first line<br><br />this is a second line<br />this is a 3rd line

since this is for a news site, i much prefer the final html to be like this:

<p>hello first line</p><p>this is a second line<br />this is a 3rd line</p>

so my current solution is this:

  1. i need to trim the $data for <br /> at the start/end of the string
  2. replace all strings that have 2 <br/> or more with </p><p> (one single <br /> is allowed).
  3. finally, add <p> at the start and </p> at the end

i only have steps 1 and 3 so far. can someone give me a hand with step 2?

function replace_br($data) {
 # step 1
 $data = trim($data,'<p>');
 $data = trim($data,'</p>');
 $data = trim($data,'<br />');
 # step 2 ???
 // preg_replace() ?
 # step 3
 $data = '<p>'.$data.'</p>';
 return $data;
}

thanks!

ps: it would be even better to avoid specific situations. example: "hello<br /><br /><br /><br /><br />too much space" -- those 5 breaklines should also be converted to just one "</p><p>"

final solution (special thanks to kemp!)

function sanitize_content($data) {
    $data = strip_tags($data,'<p>,<br>,<img>,<a>,<strong>,<u>,<em>,<blockquote>,<ol>,<ul>,<li>,<span>');
    $data = trim($data,'<p>');
    $data = trim($data,'</p>');
    $data = trim($data,'<br />');
    $data = preg_replace('#(?:<br\s*/?>\s*?){2,}#','</p><p>',$data);
    $data = '<p>'.$data.'</p>';
    return $data;
}

解决方案

This will work even if the two <br>s are on different lines (i.e. there is a newline or any whitespace between them):

function replace_br($data) {
    $data = preg_replace('#(?:<br\s*/?>\s*?){2,}#', '</p><p>', $data);
    return "<p>$data</p>";
}

这篇关于php:替换double&lt; br /&gt;与&lt; p&gt;&lt; p&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆