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 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    add line break after ever '*'

    Hi,

    I have a variable where from I get a string like this (* aaaa * bbbb * cccc)

    I need to put line break after every star * then it should like this

    * aaaa
    * bbbb
    * cccc


    How can I do it? please help!

    I used str_replace but I failed to achieve that I require

    Regards

  • #2
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    What code did you try?

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,306
    Thanks
    4
    Thanked 205 Times in 202 Posts
    PHP Code:
    $b str_replace('*',"\r\n*",'* aaaa * bbbb * cccc');
    echo 
    $b
    Dave .... HostMonster for all of your hosting needs

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    40
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by shajiuddin View Post
    Hi,

    I have a variable where from I get a string like this (* aaaa * bbbb * cccc)

    I need to put line break after every star * then it should like this

    * aaaa
    * bbbb
    * cccc


    How can I do it? please help!

    I used str_replace but I failed to achieve that I require

    Regards
    I'm going to assume you meant "before every star", as that's what your example looks like.



    Try using all single quotes or all double quotes in your code's function. Not sure if that would make a difference, but helps to be consistent.


    That said, your fuction does look correct if you are trying to add line breaks to, say, a file or a text document. If you are trying to get line breaks on a webpage, then you don't use \r\n, you use <br>... which would make the function look like this:


    Code:
    $b = str_replace('*',"<br>*",'* aaaa * bbbb * cccc');
    echo $b;

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    The code was not posted by the OP... also, double-quotes were necessary to interpolate the carriage return and new line.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by themousemaster View Post
    That said, your fuction does look correct if you are trying to add line breaks to, say, a file or a text document. If you are trying to get line breaks on a webpage, then you don't use \r\n, you use <br>... which would make the function look like this
    No don't do this. Keep it as line feeds, you can use nl2br if necessary to replace the feeds with <br />. There is no guarantee that the OP isn't intending to display this in an HTML textarea, and using <br /> would force a replacement of the <br /> to a linebreak anyway, so going the other way around is better. This will also allow the opportunity to ltrim the leading linefeed off and append a trailing one if desired.

  • #7
    New to the CF scene
    Join Date
    Jan 2012
    Location
    United States
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    PHP Code:
    <?php

            $b 
    preg_replace('#\*#''\\1'."\r\n"$b);

            echo 
    $b;

    ?>

    I have not actually tested this... I'm typing this from my iPhone. This was just off the top of my head, so no guarantees.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by createdigital View Post
    Try this:

    PHP Code:
    <?php

            $b 
    preg_replace('#\*#''\\1'."\r\n"$b);

            echo 
    $b;

    ?>

    I have not actually tested this... I'm typing this from my iPhone. This was just off the top of my head, so no guarantees.
    str_replace is substantially faster than preg, and with such a simple pattern a preg is a bit of a waste.


  •  

    Posting Permissions

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