str_replace not working as i want it to

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

str_replace not working as i want it to

Post by Da_Elf »

This is a simple problem but i cant find the answer
I'm pulling some text from the database and want to setup some bullet points.

Code: Select all

$temptext = $search['text'];
$text = nl2br(str_replace("/n/n","/n/n• ",$temptext));
echo $text
Like this is doesnt work
if i just change /n to /n•  then i get a bullet point in between where i dont want it
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: str_replace not working as i want it to

Post by Celauran »

Are you sure that /n shouldn't be \n (new line)? What is the input? The expected output?
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: str_replace not working as i want it to

Post by Da_Elf »

opps. yes \n. My code is using \n i just messed up typing here.

the result is that something like this

The first line

the second line

would look like

• The first line (• is the bullet point)
(a space here with no bullet point)
• The second line (• is the bullet point)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: str_replace not working as i want it to

Post by Celauran »

Again
Celauran wrote:What is the input? The expected output?
You haven't indicated what the input actually is. Looks like you're expecting a bullet before the first line, but it's unclear if there are two line breaks before the first line, which I suspect is actually the problem.
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: str_replace not working as i want it to

Post by Da_Elf »

oh. well the input will be from a database. it will be something that had \n line spaces in it. when i do a simple nl2br() function it correctly spaces my text. however i want it spaces but the start of each line with words will be a bullet point
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: str_replace not working as i want it to

Post by Celauran »

So it sounds like str_replace is fine and you just need to add the leading bullet. Simple enough.
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: str_replace not working as i want it to

Post by Da_Elf »

but thats what im saying. the str_replace isnt working
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: str_replace not working as i want it to

Post by Celauran »

[text]❯ php -a
Interactive shell

php > $string = "This is the first line\n\nThis is the second line";
php > echo $string;
This is the first line

This is the second line
php > echo str_replace("\n\n", "\n\n•", $string);
This is the first line

•This is the second line
php >[/text]
So you're not getting this?

Please post some exact input. Don't say it's from a database, show precisely what it is. Then, show what the actual output is.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: str_replace not working as i want it to

Post by Christopher »

Perhaps you have \r\n at the end of lines. You can remove the \r's first with;

Code: Select all

$temptext = $search['text'];
$text = nl2br(str_replace(array("\r", "/n/n"), array('', "/n/n• "), $temptext));
echo $text
(#10850)
Post Reply