Replace newlines within <ul>-tag - php

I have been battling with this for a while, because I guess I am not that awesome at regex. I have a few ul-tags and within those I would like to replace every /n newline with "" (nothing).
So, I would like to have this
other lines right here
[ul]
[li]line[/li]
[li]line 1[/li]
[li]line 2[/li]
[/ul]
other lines right here
[ul]
[li]line[/li]
[li]line 1[/li]
[li]line 2[/li]
[/ul]
other lines right here
changed to
other lines right here
[ul][li]line[/li][li]line 1[/li][li]line 2[/li][/ul]
other lines right here
[ul][li]line[/li][li]line 1[/li][li]line 2[/li][/ul]
other lines right here
I only want to get rid of the newlines within the ul-tag. How do I go about this issue I have?
Should I pre_match_all every ul tag and then format the matched content the way I would like it to be? Or is there any easier solution with preg_replace that takes care of all the newlines within the tag?
Thanks for the help already!

I'd look at it a different way. the [li]s will always be inside a [ul] (well, hopefully!). So you can just match newlines either side of those:
$string = preg_replace('/\R*(\[\/?li\])\R*/', '$1', $string);
You could do the same with a couple of str_replaces:
$string = str_replace("\r\n[li]", "[li]", $string);
$string = str_replace("[/li]\r\n", "[/li]", $string);

Related

Fixing weird indentation in text file with php

I'm taking this file and splitting it up into sentences. The issue is that its formatted weirdly. I need to remove all the random new lines, indentations and unneeded spaces. Is there a way to do this with php?
I am currently using
$test= file_get_contents("text.txt");
$stringtest = str_replace(PHP_EOL,'', $test);
But I am getting weird behavior when I try to split up the sentences. Is there a way to do this?
The weird behavior is that when I print out the text
echo $stringtest;
There are unseen characters between lines where a newline/weird_spacing used to exist.
You can use a regex to merge all whitespaces to a single space. Also you probably want to remove whitespace at the beginning and end. Try this:
$test = trim($test);
$test = preg_replace('/\s+/s', ' ', $test);

remove \n from paragraph

I have a jquery editable div that when you click on it you can edit the text. The problem is that when the data is called from the db and placed into the paragraph I keep getting a \n for every space. How can I replace the \n with an actual new line.
I tried nl2br(), but that's not very convenient for my users since they then have to play with the <br /> when they want to edit the paragraph.
Any thoughts?
What about:
str_replace("\\n", "", $str); // see if this gets rid of them
Then this should work to put actual newlines in there:
str_replace("\\n", "\n", $str); // should replace with actual newline
try:-
$strippedText = str_replace(chr(10), '', $textFromDB);
or
$strippedText = str_replace(chr(10), '<br/>', $textFromDB);
Does this work? (Working on the possiblity that the newlines are already escaped).
$strippedText = str_replace('\\n', ' ', $textFromDB);
Are you using a ready made solution or making your own? I use http://aloha-editor.org/ for stuff like this and it's mostly problem free.
Have you tried str_replace?
$myTextFromDB = str_replace('\n', PHP_EOL, $myTextFromDB);
Ok, I think this is different enough that I should do a separate answer for it.
Are you saying a literal "slash n" shows up on the page? Or are you saying that your newlines show up as spaces?
If it's the latter, then there's no way around that. HTML will show newlines only as a space - you have to convert to br tags to break the line if it's not in a textarea context. But you can always convert them back to newlines when you pop that textarea up for your user and this should work well for people.

Limit amount of br's nl2br shows in a row?

I'm having some problems with a "bb parser" I'm coding. Or, well, not with the parser itself, but the nl2br modifying it.
The string from the database is like the following:
text text text
[code]code code code[/code]
text text text
Now, nl2br puts one br / after the first "text text text", and then another one below that, so there's two line breaks before the [code] tag (which actually is correct, but not what I want).
Is there any way I can limit how many br's are entered in a row? I can't seem to find a solution that's simple enough.
Thanks in advance, guys.
In addition to previous solution, I add a different one, since Fredrik asked for it. This will replace double <br> after nl2br instead of before.
$string = nl2br( $string );
$string = preg_replace( '/(<br(?: \\/)?>\\r?\\n?\\r?)(?=\\1)/is', '', $string );
You could for example replace two linebreaks (or more) by one by using preg_replace :-)
You could use
$string = str_replace(array("\r\n\r\n", "\n\r\n\r", "\n\n", "\r\r"), array("\r\n","\n\r","\n","\r"), $string);
This prevents double <br> tags. Preg_replace as suggested before is better if there could be more than two new lines in a row.

Link converting pregmatch working with markdown

function makeLinks($text) {
$text = preg_replace('%(?<!href=")(((f|ht){1}(tp://|tps://))[-a-zA-^Z0-9#:\%_\+.~#?&//=]+)%i',
'\\1', $text);
$text = preg_replace('%([:space:]()[{}])(www.[-a-zA-Z0-9#:\%_\+.~#?&//=]+)%i',
'\\1\\2', $text);
return $text;
}
It misses if I have something like this: - www.website.org (a hyphen then a space) at the beginning of a line. If I have - www.website.org - www.website.org it catches the second one.
Shouldn't that be covered by the space in the second preg_replace?
I also tried %(\s\n\r(){})
I am running it through markdown, but not till after (markdown(makeLinks($foo))) so I thought that shouldn't interfere, but when I take the markdown off and everything just echos out in one line, it does make links out of them. If i put makeLinks(markdown($foo)) it behaves the same as initially.. not making links out of the ones that begin with www at the beginning of list items.
Thats some pretty dodgy regex work there. Here is a regex I would recommend instead for URL dectection:
%(?<!href="?)(((f|ht)(tp://|tps://))?[a-zA-Z0-9-].[-a-zA-^Z0-9#:\%_\+.~#?&//=]+)%i
Should be a lot more reliable than the two you have now.

Replacing text function in php

I want to clean up some parsed text such as
\n the said \r\n\r\n\r\n I look in your eyes my dear\r\n\r\nI see green rolling Forests\r\n\r\nI see the far away Sky\r\n\r\nThey turn into the rain\r\n\r\n\r\nI see high soaring eagles... more\n
So I want to get rid of the "\n", "\r\n", "\r\n\r\n", "\r\n\r\n\r\n", "\r\n\r\n\r\n\r\n" and "\r". That's all the combinations that appear in my parsed text.
Is there a way to do this in php?
If you just want 1 newline instead of multiple I would suggest this:
$clean = preg_replace(array("/\r+/","/(\n){2,}/"),array("","\n"),$text);
Otherwise str_replace to strip out newlines or nl2br will do the job. You could also adapt the regex to replace 1 or more newlines with a BR tag:
$clean = preg_replace(array("/\r+/","/\n+/"),array("","<br />"),$text);
What about
$text = str_replace(array("\n", "\r"), '', $text);
That will remove all new line characters.
If you want them as new lines, I'd change the replace to <br /> for HTML (or better still, use PHP's nl2br()), or standardise them in normal text with \n, for example.

Categories