Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP nl2p (one problem!)
Old 07-19-2008, 01:21 PM PHP nl2p (one problem!)
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
Have been using an nl2p function I found somewhere a while back for a long time however I've never really needed to use line breaks with it until now. Now when setting $line_breaks to true it doesn't return the <br's correctly.

It inserts the BR however it also snips off the first character of the line in the process.

Any suggestions?

PHP Code:
function nl2p($string$line_breaks true$xml false)
{
   
// Remove existing HTML formatting to avoid double-wrapping things
    
$string str_replace(array('<p>''</p>''<br>''<br />'"\r"), ''$string);
    
    
// It is conceivable that people might still want single line-breaks
    // without breaking into a new paragraph.
    
if ($line_breaks)
// ERROR IS IN THE FOLLOWING LINE SOMEWHERE
        
return '<p>'.preg_replace(array('/([\n]{2,})/i''/([^>])\n([^<])/i'), array("</p>\n<p>"'<br />'), trim($string)).'</p>';
    else 
        return 
'<p>'.preg_replace('/([\n]{1,})/i'"</p>\n<p>"trim($string)).'</p>';

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
 
Register now for full access!
Old 07-25-2008, 12:54 PM Re: PHP nl2p (one problem!)
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
bump..
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 08-01-2008, 04:39 PM Re: PHP nl2p (one problem!)
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
I regret to.. bump.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 08-02-2008, 06:46 PM Re: PHP nl2p (one problem!)
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
How newbie of me. And the solution is:

PHP Code:
function nl2p($string)
{
    
$string "<p>" $string "</p>";
    
$string preg_replace("/\r\n\r\n/""</p>\n\n<p>"$string);
    
$string preg_replace("/\r\n/""<br />"$string);
    return 
$string;

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 08-03-2008, 04:58 PM Re: PHP nl2p (one problem!)
Novice Talker

Posts: 6
Name: Caleb Mingle
Trades: 0
Would probably easier to use nl2br() http://us.php.net/nl2br
TehUpload is offline
Reply With Quote
View Public Profile
 
Old 08-03-2008, 07:14 PM Re: PHP nl2p (one problem!)
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Thanks for sharing your solution Rich.

Caleb: He's not tring to create <br />'s, but <p></p> surrounding things where there are 2 newlines and <br />'s where there is only 1 newline, so a bit more is needed. That said, $string = preg_replace("/\r\n/", "<br />", $string); could be replaced with nl2br b/c it occurs after the double newline replacement code.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-03-2008, 07:17 PM Re: PHP nl2p (one problem!)
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Hey, wait a minute:
PHP Code:
$string preg_replace("/\r\n\r\n/""</p>\n\n<p>"$string);
$string preg_replace("/\r\n/""<br />"$string); 
Just thinking here. But, the first line quoted above inserts 2 newlines between paragraph closing and opening. the second then replaces those newlines with <br />'s (amongst other newlines). That seems flawed.

Perhaps what you want to do is format the source code w/o a visual change there? If so, then first replacing double newlines with </p><p>, then replacing the single newlines, and then replace </p><p> with the newlines added between?

Perhaps I misunderstand the point. I've been watching this thread from the beginning, just could figure out fully what you wanted to do and reading the initial reg exs confused me.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-03-2008, 09:22 PM Re: PHP nl2p (one problem!)
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
Works fine for me.

HTML Code:
Hello\r\n\r\nHow are you?\r\nNot bad!
Becomes

HTML Code:
<p>Hello</p>

<p>How are you?<br />
Not bad!</p>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Reply     « Reply to PHP nl2p (one problem!)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23747 seconds with 12 queries