I am trying to remove these \n\n characters that appear in my string when I post a form.
My code is;
$text = $textdb['columnname'];
echo trim(preg_replace('/\s\s+/', ' ', $text));
But it doesn't work. I have tried literally every example here and still nothing works. Am I suppose to do something prior to this? Does this work with a certain version of php? Am using 5.6 by the way. Thanks.
Just use nl2br
Example:
$text = nl2br("hey\n neighbor");
would print
hey
neighbor
Related
I got a Button, that creates this template code
[5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)
The filename and file size vary und the user can change the ##LINKNAME##, as well.
This code goes to a database and when I get it back, I want to replace it to
##LINKNAME## <i>(96.51 kb)</i>
I think I need to use preg_replace() but I am not really good at regular expressions.
I stopped here:
<?php
$string = ' [5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)';
$regex = '[[a-zA-Z0-9]+.pdf](.*?)\s';
$replace = 'I DONT KNOW';
echo preg_replace($regex, $replace, $string);
?>
I know that this is a complete mess, but I'm not getting any results as long as I don't know the regex and the correct $replace.
Regex: ^\[([^\]]+)\](\w+)\s\(([^)]+)\)$
Replace with: \2<i>(\3)</i>
Demo
I'm testing a preg_replace function, and I return from an ajax function the processed data (after I process the data through preg_replace, I put it through htmlentities() ):
My test string is:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
I'm trying to make sure all those characters aren't replaced. My replace function is:
$string = preg_replace('/[^a-zA-Z0-9\s+\n\r,.\/~!##\$%\^&*()\+={}\[\]|:;<>?\'"-]/', '', $string);
I return both the data from "echo" and after going through htmlentities() to see the difference.
when I return the data using alert(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
respectively. However, when I put either of those into $("#div").html(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
so the multiple spaces are lost. Why does the .html() function reduce the spaces? And how can I fix this? Thanks
remove "\s+" from your regular expression and try again
"I'm trying to make sure all those characters aren't replaced." you mean it?
so i make a test like below:
$string = "~!##$%^&*()-+={}|[]:;< >?";
// $string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
echo "'", $string, "'";
output is
'~!##$%^&*()-+={}|[]:;< >?'
if you want keep whole white space between "<" and ">" in $string, i can say that no way, if you wanna output the same white spaces as you input: there are two way can make this:
1> use <pre> tage
2> use replace the white space
does you want these? if you want to keep all? why use regular?
I have tried quite a few things so far with no luck:
I am using TinyMCE, which I run through mysql_real_escape_string() then add to database.
heres an example of the string thats stored in the DB:
<p>It would be nice to be able to put this in 2 categories....</p>\n<p>something to think about.</p>
I retrieve the data then I get problems. I can't get rid of the \n
$string = the database entry listed above
$string = substr($item['body'], 0, 120). "...";
$item['bodysum'] = nl2br(stripslashes(str_replace("\n", "<br />", $string)));
Here is a pic of the output.
I just want it to be normal HTML. If possible I'd like to convert it all to one line as well instead of making the section larger. Its supposed to be a summary of what someone posts, so having it make the summary area larger for 1 word then a new line doesn't make sense!
Is that "\n" a literal "\" followed by "n"? If that's the case, then try:
$item['bodysum'] = nl2br(str_replace("\\n", "<br />", $string));
Try first strip_slashes than nl2br
$item['bodysum'] = nl2br(stripslashes($string));
Try this
$text = '<p>It would be nice to be able to put this in 2 categories....</p>\n<p>something to think about.</p>';
echo preg_replace('#(\\\r|\\\r\\\n|\\\n)#', '<br/>', $text);
EXAMPLE HERE
I'm using str_replace and it's not working correctly.
I have a text area, which input is sent with a form. When the data is received by the server, I want to change the new lines to ",".
$teams = $_GET["teams"];
$teams = str_replace("\n",",",$teams);
echo $teams;
Strangely, I receive the following result
Chelsea
,real
,Barcelona
instead of Chealsea,real,Barcelona.
What's wrong?
To expand on Waage's response, you could use an array to replace both sets of characters
$teams = str_replace(array("\r\n", "\n"),",",$teams);
echo $teams;
This should handle both items properly, as a single \n is valid and would not get caught if you were just replacing \r\n
Try replacing "\r\n" instead of just "\n"
I would trim the text and replace all consecutive CR/LF characters with a comma:
$text = preg_replace('/[\r\n]+/', ',', trim($text))
I had the same issue but found a different answer so thought I would share in case it helps someone.
The problem I had was that I wanted to replace \n with <br/> for printing in HTML. The simple change I had to make was to escape the backslash in str_replace("\n","<br>",($text)) like this:
str_replace("\\n","<br>",($text))
I have tried and tried and tried now.
I have a mysql field (TEXT) which contains the value of a textarea.
That is, if the text is written EXACTLY like this:
Hello. Hello.
Hello.
Hel
lo.
Then thats how it will appear in the mysql field because Im using wordwrap and nl2br functions when inserting it there.
Now, I am creating a dynamic page, where the 'meta description content' is updated with the 'TEXT' content, BUT, in one long string without any breaks and new lines.
That said, I need to make text to be in one string.
I have used this:
str_replace ("<br/>", "", $string);
This displays the text as one string, yes, but when viewing the source code of the page, you can see that the breaks are there, like this:
<meta name="description" content="
Hello. Hello.
Hello.
Hel
lo." />
I have also tried replacing the with '\n' and others, without success...
How can I solve this irritating problem?
Let me know if you need more input, and I will update this Q.
Replace all groups of whitespace with a single space:
$output = preg_replace('!\s+!', ' ', $input);
Try replacing "\n" with "" instead.
handy function:
function removeEmptyLines($s) {
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $s);
}
You might try to something like this str_replace("\n\r", "", $string); I have found that sometimes the return character \r is hiding in there someplace.