I am creating a format tool that strips content from articles for print. The demo can be seen here. The full source is avaliable here.
Right now the tool strips formating and can also keep paragraphs by using nl2br
What I would like to do is to be able to shift the content to the left and only have a paragraph if there is a break between the content.
for example:
This
is the first paragraphSecond Paragraph
Becomes:
this is the first paragraph
Second Paragraph
I tried this by using regex to check if there were two spaces at the end but that didn't work. Here is some sample code: HTML:
<form method="post" action="">
<textarea cols="68" rows="21" name="textinput"></textarea><br/>
<input type="checkbox" name="keep_paragraphs" value="true" checked /> Keep Paragraphs<br/>
<input type="checkbox" name="shift_left" value="true" /> Remove whitespace after line unless it ends in two spaces<br/>
<input type="submit" value="Submit" />
</form>
PHP:
$text= $_POST['textinput'];
$p= $_POST['keep_paragraphs'];
$lb= $_POST['shift_left'];
if(get_magic_quotes_gpc()){
$text = stripslashes($text);
// strip off the slashes if they are magically added.
}
$text = htmlentities($text);
//if we should keep formatting
if($p=="true"){
$text =nl2br($text开发者_高级运维);
}
if($lb=="true"){
$text = preg_replace('/\s+/', ' ', trim($text));
}
echo $text;
Any help on this would be great
EDIT: include example
POST textbox = "Hi Jane
How are You doing today
I hope all is well";
Most text will be coming from e-mails and other sources,basicly it needs to be super genderic.
The regex you need is,
/(?<!\r\n)\r\n(?=\w)/
replace it with a space.
Update
A small correction,
$text ="This
is
a
paragraph
Second Paragraph";
$lb = "true";
if($lb=="true"){
$text2 = preg_replace('/(?<!\r\n)\r\n(?=\w)/', ' ', trim($text));
}
echo $text2;
Your can write this
$text = preg_replace('@\n([a-z])@Us', ' \1', trim($text));
精彩评论