Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Location
    UK
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newline to BR (not using nl2br())

    How would I convert a section of text including HTML code (from, say, a database) so that newlines were preceeded with <br> (or the XHTML comlient <br />) but didn't put these linebreak tags in the middle of other HTML tags such as <img> tags?

    nl2br() will change:
    Code:
    This is
    some text
    <img src="http://blah.com/img.gif"
    title="Image">
    into
    Code:
    This is<br />
    some text<br />
    <img src="http://blah.com/img.gif"<br />
    title="Image">
    The <img> tag will obviously not work with the line break stuck in the middle of it.

    After a bit of thiking I came to the conclusion that I'd have to use a Regex but the precise regex eluded me and continues to do so. Therefore I have turned to others for help

    Thanks in advance

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    I hate these type of answers , but the best bet would be to not put the newlines into the DB in the first place ?

    nl2br(); only adds a break where a newline exists , no linebreak == no <BR/>

    we have at least one resident regex guru ... but if you are using regex anywhere I would suggest you should be doing it B4 you add to the database , not at display time.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Location
    UK
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah I thought that using the regex before adding to the database would be faster etc but whether it's while inserting or while displaying i still need the regex

    Currently I have to enter the <br> tags manually while typing into the textarea which is a bit of a pain, I was just trying to make things easier.

    Any regex I came up with myself managed to match either ALL the instances of <br> or NONE of them (this was after the nl2br()), neither of which are particularly useful...

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The difficult part is to locate those tags which contain newlines, the replacement functionality is easy then. Here's a quick hack that shows one way to do this:

    Code:
    $str = 'This <b id="
    foo">is</b>
    some text
    <img src="http://blah.com/img.gif"
    title="Image">';
    
    function stripNewlines($matches) {
    	//	replace newlines
    	$tag = str_replace(array("\n", "\r"), '', $matches[0]);
    	//	add spaces between very near standing attributes
    	$tag = preg_replace('/([^=])(["\'])(\w)/', '\1\2 \3', $tag);	    
    	
    	return $tag;
    }
    
    function cleanText($text) {
    	return preg_replace_callback('/<.*?\n.*?>/', "stripNewlines", $text);
    }
    
    
    //	test output
    var_dump(cleanText($str));
    echo "\n\n";
    var_dump(nl2br(cleanText($str))); // how it looks like after nl2br() is applied to the text
    Note: The forum software tends to strip special characters from regexes. If you don't get the desired output, tell me, and I try to upload a correct version.
    De gustibus non est disputandum.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •