English 中文(简体)
预兆
原标题:preg_replace and linebreaks

我有一个很大的文本领域,我想改为有效的超文本。 这包括在每个新段落中增加一个段落,我如何用购买力平价这样做? 例如,添加一条线,就如:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

goes to

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>

能否用预想——替代?

Thanks

最佳回答

我认为这是你想要的:

  function nl2p($input) {
    return preg_replace( ~^s*(.*?)s*$~sm ,  <p>$1</p> , $input);
  }
问题回答
$result = "<p>" . str_replace("
","</p><p>",$text) . "</p>";

(demo)

Maybe this helps http://php.net/manual/de/function.nl2br.php#78883

<?php
function nls2p($str)
 {
   return str_replace( <p></p> ,   ,  <p>  
         . preg_replace( #([
]s*?[
]){2,}# ,  </p>$0<p> , $str) 
         .  </p> );
 }
?>

This way you don t have to worry about the different types of vertical line breaks ( or or f or a combination):

$output =  <p>  . preg_replace( /v+/ ,  </p><p> , $input) .  </p> ;

You might also find this site helpful for testing: http://www.gummydev.com/regex/





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签