PHP Inserting newlines automatically - php

I have a php data migration script that serializes an array of text. Somehow, and this is not in my codebase, but the script is inserting \\\\\\\r\\\\\\\n into the string. Here is an example of what the output is like:
Product Line: [56313] LEGO Batman Screenshot\\\\\\\\r\\\\\\\\n[5
6384] LEGO Batman Screenshot[56446] LEGO Batman Screenshot[56460] LEGO Batman: T
There are no line breaks in between the different products (as you can see, there are two products being shown there - that's two iterations). When the second product gets appended to the first, the \\\\\\r\\\\\\n gets prepended. This question is fairly difficult to explain, and I don't really think that any code will help explain it. What is this and how do I get rid of it? I'm on a windows platform.

You can call trim(), http://us.php.net/trim

Hard to tell what's going on without the code, but I would search the code for \r and \n and try and find out where this is being inserted.

There are various ways to trim that string back to normal, but I suggest you search for the cause of why this happens.
The many backslashes look like the result of a (multiply) botched addslashes() operation, or a string being mangled by magic_quotes_gpc. The data definitely started with a line break somewhere along the line - if you got the data from a text file, presumably from there. These are two separate issues, even if you manage to remove the newlines, the slashes issue remains.
You would have to show us more code, or examine it using test output or a debugger.

Related

Where mysterious no-break line %a0 breaking JSON coming from?

I give up trying to solve this by myself, I need help! I have been working on a WordPress project that has a few features working with AJAX. After updating PHP to 5.6 (as the latest WordPress requested), many of my AJAX functions are broken because of a mysterious no-break line character %a0 appearing in its response and breaking the JSON structure.
The response is from json_encode().
JSON response I am getting:
{
"term_id":75,
"name":"iPhone
3G",
"slug":"iphone-3g"
},
Investigation:
After many hours of reading about this, I tried several solutions that worked for others, but they didn't work for me.
Turning off magic_quotes_gpc in php.ini
Escape the string with preg_replace on the server side for all different no-break/new-line special characters
Escape the string on client side with str.replace
I checked the database, and there is no %a0 for that entry, there is a space %20, which is correct. I also noticed that if I remove that space, this happens to the next item that has a space %20.
I should also mention that this example above with the iPhone 3G is not unique. After this item, a few items are cleared (even those with space %20) but then it happens again later on down the latter with other items, same situation.
So it appears that PHP is replacing %20 with %a0 every so often.
What should I do?

How to force line-breaks on ?

Sometimes text on my pages looks very strange, real example:
trained professionals and paraprofessionals coming together
...While the parent div is quite narrow so the text is just sticking out of it.
And it looks quite strange, because actually represents a space.
So, I wonder if it's possible to make the browser account these characters as actual spaces and break the line where necessary without actually replacing them?
EDIT
Why a blind replacing is a problem?
Because may be needed sometimes.
Consider the following example:
Ranks:<br>
Marshall<br>
Leutenant<br>
Sergeant
If I just use a preg_replace on them it would look differently in the end.
(I would also consider some suggestions if you have any ideas on replacing them smartly (for php platform) If you could think of some algorithm that wouldn't affect formatting.)
By definition, is a non-breakable space. It's very meaning is not to be broken across line endings. If this is not what you intend then I suggest fixing the HTML instead of trying to force the browser into non-standard behaviour.

MySQL Whitespace Issues

I'm working with a client to convert his Access database into a MySQL one. He also has a website linked to the database...here is where the problem lies:
The fields in the database have too many whitespaces. EX: Instead of having space like this:
4.0L Engine
the spacing is like this:
4.0L Engine
This causes browsers like IE and Firefox to throw an error because it looks like they are displaying the incorrect amount of whitespace.
I know one solution is to go in and manually fix the whitespacing but it has 1000's of records and it will waste too much time.
I'm using CodeIgniter for his website to select the values from the database. Is there anyway I can make CodeIgniter, IE, and Firefox display the correct number of whtiespaces or automate the process of correcting the whitespaces without manually doing them?
Thanks,
Dro Sarhadian
Edit: Here is an example of an actual string in the database:
3.2L 196 OHV ENG - 1956-65 (CAST IRON ENG)
and here is how it's shown in firefox/IE
3.2L 196 OHV ENG - 1956-65 (CAST IRON ENG)
I've never heard of an error being thrown because of white space, but you could always create a script that
pulls down the record from the database
manipulates the data through regular expressions -- looking for extra white space and replacing it with the correct white space
then updates the record in the database
If you loop through all of the records, this would only need to be done once. Then the data in the database is as you intend.

Is it possible to write a regex which checks if a string (javascript & php code) is minified?

Is it possible to write a regular expression which checks if a string (some code) is minified?
Many PHP/JS obfuscators remove white space chars (among other things).
So, the final minified code sometimes looks like this:
PHP:
$a=array();if(is_array($a)){echo'ok';}
JS:
a=[];if(typeof(a)=='object'&&(a instanceof Array){alert('ok')}
in both cases there are no space chars before and after "{", "}", ";", etc. There also some other patterns which can help. I am not expecting a high accuracy regex, just need one which checks if at least 100 chars of string looks like minified code.
Thanks in advice.
PURPOSES: web malware scanner
I think a minifier will strip all newline characters, although there might possibly be one at the end of the file still if the minified code was pasted back in a text editor. Something like this will probably be fairly accurate:
/^[^\n\r]+(\r\n?|\n)?$/
That just tests that there are no newline characters in the whole thing except for possibly one at the end. So no guarantees, but I think it will work well on any longish block of code.
The short answer is "no", regex cannot do this.
Your best bet will probably be to do a statistical analysis of the source files, and compare against some known heuristics. For instance, by comparing the variable names against those often found in minimized code. A minimized file probably has a lot of one-character variable names, for instance... and won't have two-character variable names until all the one-character variable names are exhausted... etc.
Another option would be simply to run the source file through a minimizer, and see if the output is sufficiently different from the input. If not, it was probably already minimized.
But I have to agree with sg3s's final sentence: If you can explain why you need this, we can probably provide more useful answers to your actual needs.
No. Since the syntax/code and its intention doesn't change and some people who're very familiar with the php and/or js will write simple functions on one line without any whitespace at all (me :s).
What you could do is count all the whitespace characters in a string though this would also be unreliable since for some stuff you simply need whitespace, like x instanceof y heh. Also not all code is minified and cramped into a single row (see jQuery UI) so you can't really count on that either....
Maybe you can explain why you need to know this and we can try and find an alternative?
You can't tell if it's got minified or just written like that by hand (probably only applies for smaller scripts). But you can check if it doesn't contain unnecessary whitespace.
Take a look at open source obfuscator/minifier and see what rules they use to remove the whitespace. Validating if those rules were applied should work, if regex get to complex, a simple parser might be needed.
Just make sure that string literals like a="if ( b )" are excluded.
Run it through a parser for that particular language (even a prettifier might work fine) and modify it to count the number of unused characters. Use the percentage of unused chars vs. number of chars in documents as a test for minification. I don't think you can do this accurately with regex, although counting whitespace vs. document content might be okay.

Form multiplies Inline

I've bumped into a small problem that i cant seem to find the fix for.
I've got this simple form with a textarea, no biggie.
The problem shows when i edit a post i've already got in the database.
So, i retrieve the varchar field i have in my database and inserts it into my form by default.
If this is what i got by default:
this is some default text
with a line break
now, if i just saves this without doing any changes, this is what i get in the PHP $_POST variable:
this is some default text
with a line break
the /n character seem to multiply by itself. The next time i save the text the /n multiply again, giving me 4 /n in a row and so on.
It doesn't make any sense to me.
Step 1. I pre-fill the textarea with the contents mentioned in the first gray box above
Step 2. I submit the same content
Step 3. I run the following code before i do anything else:
echo '<pre>'. $_POST['content'] .'</pre>'; die;
Step 4. The newline are multiplied as in the second gray box above
That's easy.
there is some code in your application, that doubles line breaks. Just find it and get rid of it. Not a big deal.
(If you don't trust me, as you obviously do, create a simple testing script consists of just form and nothing else and see)
Wild guess: Windows CR LF line breaks.
In Windows, if you type a line break, they consist of not only \n, but \r\n. If MySQL implicitly converts \r to \n when inserting/updating, this would explain this behaviour.
Try removing any occurences of \r in the PHP first and see if that helps.
Remove line break using trim

Categories