Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-03-2012, 12:32 PM   PM User | #1
shajiuddin
New Coder

 
Join Date: Sep 2011
Posts: 69
Thanks: 6
Thanked 0 Times in 0 Posts
shajiuddin is an unknown quantity at this point
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
shajiuddin is offline   Reply With Quote
Old 01-03-2012, 12:35 PM   PM User | #2
melloorr
Regular Coder

 
Join Date: Dec 2011
Location: NW England
Posts: 194
Thanks: 8
Thanked 15 Times in 15 Posts
melloorr is an unknown quantity at this point
What code did you try?
melloorr is offline   Reply With Quote
Old 01-03-2012, 12:38 PM   PM User | #3
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
PHP Code:
$b str_replace('*',"\r\n*",'* aaaa * bbbb * cccc');
echo 
$b
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 01-03-2012, 03:25 PM   PM User | #4
themousemaster
New Coder

 
Join Date: Sep 2011
Posts: 40
Thanks: 0
Thanked 7 Times in 7 Posts
themousemaster is an unknown quantity at this point
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;
themousemaster is offline   Reply With Quote
Old 01-03-2012, 03:36 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
The code was not posted by the OP... also, double-quotes were necessary to interpolate the carriage return and new line.
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 01-03-2012, 06:19 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-03-2012, 09:01 PM   PM User | #7
createdigital
New to the CF scene

 
Join Date: Jan 2012
Location: United States
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
createdigital is an unknown quantity at this point
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.
createdigital is offline   Reply With Quote
Old 01-03-2012, 09:08 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 04:52 PM.


Advertisement
Log in to turn off these ads.