Page 1 of 1
str_replace not working as i want it to
Posted: Tue Nov 08, 2016 5:19 am
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
Re: str_replace not working as i want it to
Posted: Tue Nov 08, 2016 7:32 am
by Celauran
Are you sure that /n shouldn't be \n (new line)? What is the input? The expected output?
Re: str_replace not working as i want it to
Posted: Tue Nov 08, 2016 8:19 am
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)
Re: str_replace not working as i want it to
Posted: Tue Nov 08, 2016 8:40 am
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.
Re: str_replace not working as i want it to
Posted: Tue Nov 08, 2016 10:26 am
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
Re: str_replace not working as i want it to
Posted: Tue Nov 08, 2016 10:29 am
by Celauran
So it sounds like str_replace is fine and you just need to add the leading bullet. Simple enough.
Re: str_replace not working as i want it to
Posted: Tue Nov 08, 2016 10:36 am
by Da_Elf
but thats what im saying. the str_replace isnt working
Re: str_replace not working as i want it to
Posted: Tue Nov 08, 2016 10:48 am
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.
Re: str_replace not working as i want it to
Posted: Tue Nov 08, 2016 10:57 am
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