Ello chaps.
Ok - cleaning my string like this:
$clean_string = preg_replace("/[\n\r]/"," ",trim($dirty_string));
.. Echo it out to the screen and it works - one lovely big lump of text with no newlines.
INSERT it into a LONGTEXT type field in MySQL - then preview the resulting data in Sequel Pro - and for some reason it has new lines. Loads of them, just like it did when it was new, before I cleaned it.
What am I doing wrong? Is it LONGTEXT?
This is what the html source looks like when I use the content of a SO page as a the string - note the many spaces and newlines - even when cleaned!
mysql - Trouble with CONCAT and Longtext - Stack Overflow
Stack Exchange
Solved by using this:
$clean_str = str_replace(array("\r", "\n"), '', $dirty_string);
BUT - replacing double quotes for single quotes. Works now.
$clean_str = str_replace(array('\r', '\n'), '', $dirty_string);
Thanks mfonda for getting me close!
That should work. Also there really isn't any need to use preg_replace here-- $clean_str = str_replace(array("\r", "\n"), '', $dirty_string) will work just fine, and will be a little faster.
$clean_string = preg_replace('/\s+/m', ' ',trim($string)); will replace every "space sequence" into a single space. Try it and see if it works as expected.
How about trimming AFTER removing all newlines?
$clean_str = trim(str_replace(array("\r", "\n"), '', $dirty_string));
Related
I'm calling same text for php but result is chracter problem. e.g;
"<div class="open"><?php echo ex_substr(180); ?></div>"
and run the function
function ex_substr($char) {
$title= get_the_excerpt($post->ID);
$title= strip_tags(substr($title,0,$char));
echo $title."...";
}
At the end of the text content,there are character problems for example: �
I'm very new about php.
as I said in the comments when you see weird stuff like � you probably have some weird non UTF-8 characters in there.
While I am by no means an expert at encodings ( typical I just remove that junk ) This is what I use to deal with them
preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
Here is a PHP sandbox you can try it in.
http://sandbox.onlinephpfunctions.com/code/695ad38ae46ef5a142102dd6150fd84279b1a058
$string ='this is a string �';
echo preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
Output
'this is a string '
You can smack it with trim($string) if that extra space at the end bugs you.
Another trick is to replace it with a space . and then remove any multiple spaces. That way if it's between words it doesn't just jam them together.
$string ='this is a�string';
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', ' ', $string);
echo trim( preg_replace('/\s{2, }/', ' ', $string)); //replace 2 or more spaces with a single space.
Even when the DB charset set to UTF-8 I occasionally get weird stuff in my DB. We have a lot of content that is scrapped off the web so who knows what that crap is.
This is sort of a "rudimentary" way to fix it, but if you're not worried about loosing those characters than it should be fine. I should study up more on encoding issues, now that I don't have PHP6 to fix all that for me.
( PS from what i understand PHP6 was supposed to have more support for multi=byte strings, but because of the short comings of C it all fell apart, which is why we went from PHP5 to PHP7 )
Hope that helps.
$title= strip_tags(substr($title,0,$char));
instead of
$title= mb_substr($title,0,$char);
The problem is completely resolved when I use it.
I'm making a CSV file, one of the database filed has stored a section called comments/note which obviously have some commas and line breaks in it too. Looked around the web found usage of preg_replace(), not much familiar with regular expressions there fore combined two different ones and not getting anything in result its totally blank and i know all records have some sort of comments in it
this i used
preg_replace( "/\r|\n/|/[,]/", "", $string )
Please what do I need to do here get one text back without line breaks and commas
Regards
You can do it using strip_tags() and preg_replace();
$clean_str = trim(preg_replace('/\s\s+/', '', strip_tags($string)));
or try this
$clean_str = str_replace(array("\r\n","\r","\n", ","), '', strip_tags($string));
I have a string
"PRINT CHIFFON<BR /><BR />
"
I am trying to remove any newline symbols and tags.
$i->colour_code = str_replace('<br>', '', strip_tags($i->colour_code));
$i->colour_code = str_replace('<br \/>', '', strip_tags($i->colour_code));
$i->colour_code = preg_replace("/[\n\r]/","",$i->colour_code);
That still does not work. Any ideas will be appreciated as this issue causes the whole system stop working
You don't need to escape the slash in the second line of replacements. In fact, since you're running it through strip_tags, you don't even need the first two replacements. Try this:
$i->colour_code = str_replace(Array("\n","\r"),"",strip_tags($i->colour_code));
Just make sure that $i->colour_code is not readonly.
What do you mean it does not work? I just made a test and your code does indeed work, you have a problem with $i->colour_code.
Since you seem okay to use preg_replace, I suggest using this line of code:
$i->color_code = preg_replace('/(<br>|<br ?\/>|\r|\n)/i', '', $i->color_code);
Unlike your examples, it will be case insensitive as well, so it doesn't matter if it is br or BR.
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.
I've tried about everything to delete some extra \n characters in a web application I'm working with. I was hoping someone has encountered this issue before and knows what can be causing this. All my JS and PHP files are UTF-8 encoded with no BOM.
And yes I've tried things like
In JS:
text.replace(/\n/g,"")
In PHP:
preg_replace("[\n]","",$result);
str_replace("\n","",$result);
and when I try
text.replace(/\n/g,"")
in the firebug console using the same string I get from the server it works but for reason it doesn't work in a JS file.
I'm desperate, picky and this is killing me. Any input is appreciated.
EDIT:
If it helps, I know how to use the replace functions above. I'm able to replace any other string or pattern except \n for some reason.
Answer Explanation:
Some people do and use what works because it just works. If you are like me and for the record I always like to know why what works WORKS!
In my case:
Why this works? str_replace('\n', '', $result)
And this doesn't? str_replace("\n", '', $result)
Looks identical right?
Well it seems that when you enclose a string with a character value like \n in double quotes "\n" it's seen as it's character value NOT as a string. On the other hand if you enclose it in single quotes '\n' it's really seen as the string \n. At least that is what i concluded in my 3 hours headache.
If what I concluded is a setup specific issue OR is erroneous please do let me know or edit.
In php, use str_replace(array('\r','\n'), '', $string).
I guess the problem is you also have \r's in your code (carriage returns, also displayed as newlines).
In javascript, the .replace() method doesn't modify the string. It returns a new modified string, so you need to reference the result.
text = text.replace(/\n/g,"")
Both of the PHP functions you tried return the altered string, they do not alter their arguments:
$result = preg_replace("[\n]","",$result);
$result = str_replace("\n","",$result);
Strangely, using
str_replace(array('\r','\n'), '', $string)
didn't work for me. I can't really work out why either.
In my situation I needed to take output from the a WordPress custom meta field, and then I was placing that formatted as HTML in a javascript array for later use as info windows in a Google Maps instance on my site.
If I did the following:
$stockist_address = $stockist_post_custom['stockist_address'][0];
$stockist_address = apply_filters( 'the_content', $stockist_address);
$stockist_sites_html .= str_replace(array('\r','\n'), '', $stockist_address);
This did not give me a string with the html on a single line. This therefore threw an error on Google Maps.
What I needed to do instead was:
$stockist_address = $stockist_post_custom['stockist_address'][0];
$stockist_address = apply_filters( 'the_content', $stockist_address);
$stockist_sites_html .= trim( preg_replace( '/\s+/', ' ', $stockist_address ) );
This worked like a charm for me.
I believe that usage of \s in regular expressions tabs, line breaks and carriage returns.