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.
Related
I have problem with using get_file_content after preg_match and str_replace.
When I read from base, text look like:
Some text with shortcodes: [formularz {"agree":"1,1,1","option":"firstname,lastname,phone,email,company","leadfrom":"Allegro"}] [formularz {"agree":"0,1,0","option":"firstname,lastname,phone","leadfrom":"Allegro"}]
Then I looking my regular '[]':
preg_match_all('/\[formularz (.*)\]/',$campaign['content'],$matches);
Then I replace some chars and words:
echo str_replace($matches[0],str_replace([':','","','"','{','}'],['=','&','','file_get_contents("http:// mydomain.com/form.php?id_added_to_lead='.$id_added_to_lead.'&','")'],$matches[1]),str_replace(array("\r\n", "\r", "\n"), "",stripslashes($campaign['content']))) ;
And finally I have:
Some text with shortcodes:
file_get_contents("http:// mydomain.com/form.php?id_added_to_lead=100&agree=1,1,1&option=firstname,lastname,phone,email,company&leadfrom=Allegro")
file_get_contents("http:// mydomain.com/form.php?id_added_to_lead=100&agree=0,1,0&option=firstname,lastname,phone&leadfrom=Allegro")
What should I do, If I want to execute file_get_content in content?
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);
I have a textarea submitting to my database on a website that is properly working. But when I generate a CSV (via PHP) from my database, all line breaks will mess up with the resulting CSV. Any CSV reader will interpret the line break from the input into a new line.
I have tried the following approaches:
Encapsulating the fields in quotation marks.
This:
$field = str_replace(array('\n', '\r', '\r\n', '\n\r'), ',', $original_field);
Also this:
$field = strip_tags(nl2br($original_field));
Combining all approaches above.
Anyhow, the ending result will still be a messed up CSV that will break on any line break inputted by user. I have managed to block new line breaks from the text area, but there's a lot of legacy submissions that need me to fix this on the CSV side as well.
Why is it not working? How can I fix this issue?
Before accepted answer (of user user1517891) is not correct, it will replace in string twice, when there is \r\n... It will replace it as two commas ,. First it will replace \r => ,, then \n => ,.
You need to use it in different order, as:
$field = str_replace(array("\r\n", "\n\r", "\n", "\r"), ',', $original_field);
Use double quotes:
$field = str_replace(array("\n", "\r", "\r\n", "\n\r"), ',', $original_field);
I'd suggest using preg_replace() for this rather than str_replace(). The reason is that there may be multiple newlines and combinations of \r and \n, and I would expect that you'd want to replace them all with just a single comma.
I'd also suggest using trim() to remove trailing blank lines.
$field = preg_replace('/[\n\r]+/', ',', trim($original_field));
You have to put \n and similar tags in double quotes otherwise they will be treated as simple strings and not as linebreaks.
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.
Currently, I'm using the following code:
preg_replace('/\s+/m','<br>',$var);
to replace line ends with <br> elements. An example of what I want:
Text Text Text
Text Text Text
Text Text Text
Should end up being:
Text Text Text<br>Text Text Text<br><br>Text Text Text
This does what it needs to, but I'd like to recognize when there is a double space and add two breaklines, instead of a single one. How can I do this, while retaining the current effect for single break lines?
I'm not too familiar with how preg_replace() works and I actually had to get help here to get that function in the first place. I took a look in the PHP manual and the function seemed a little confusing. Would anyone know of a site where I could learn how it works correctly?
You can simply replace each end-of-line character with <br />:
$var = str_replace(array("\r\n", "\n"), '<br />', $var);
There is no need to use a regular expression, but if you really want, you can use preg_replace to achieve the same effect:
$var = preg_replace("/\r?\n/", '<br />', $var);
you can do this by adding the g-modifier to the preg_replace like so:
preg_replace('/\s+/mg','<br>',$var);
preg stands for Perl Regular Expression - you'll find a lot more examples with this search string, e.g. this site or this site (I'm actually unsure, what the m-modifier does?)
Alternatively, you could use the simple $var = str_replace(' ', '<br', $var). I'm unsure, which one is faster.
Edit: If you want to replace newlines with html-breaks, use the nl2br() function.
php has a built in function for this
echo nl2br( $var );
this does \n, \r\n, \r, and \n\r
http://www.php.net/manual/en/function.nl2br.php
Check out this one. Not sure you are asking for that, but at least it works for me
$input = <<<DOC
Test Test Test
Test Test Test
Test Test Test
DOC;
$output = preg_replace("/$/m","<br/>",$input);
echo $output;
Hovewer, nl2br does just the same.
Is this what you’re trying to do?
<?php
$text = <<<EndText
Text Text Text
Text Text Text
Text Text Text
EndText;
$text = str_replace("\r", "", $text);
$text = str_replace("\n", "<br>", $text);
echo $text;
?>