Pushing to github - strange white spaces - php

When I push my code into github repo and display random files, they contain some strange whitespaces, e.g.:
// 'auth' => MPATH.'auth', // Authentication module
// 'database' => MPATH.'database', // Database access
In my IDE the code is perfectly lined up, on github - it behaves like above, in totally random places. Is there any way to fix this?
I use tabs for indents.

I would suggest using spaces over tabs going forward. You can set your editor to input 2, 4 or how many spaces each time you hit Tab.I believe this will save you much headache, because a space is always exactly the same width, whereas tab width can always change depending on context.
For now you can convert the tabs like this
expand -t2 foo-tabs.php > foo-spaces.php

Related

Vim replace text on the fly ( "." => "->")

I have this project in php I am working on and I find very annoying to type -> to call methods. I want to make vim do the job for me. I want it to replace [^\s]. (non space + dot) with ->.
Also, I the dollar var prefix annoys me and I wish I could replace \s[a-z_] (white space followed by lower A to Z and underscore) with $ + the next letter I typed.
I.E.
a #=> $a
foo + bar # => $foo + $bar
this #=> $this
this.myMethod() #=> $this->myMethod()
That should happen only in php files, of course.
Is there a way to accomplish that? Something a little fancier than abbreviations maybe.
For the first problem:
inoremap . ->
in your .vimrc would suffice. If you want it specific to php files:
filetype on
autocmd FileType php inoremap . ->
As for the second, I don't really see a way this can be written to work automatically.
I am not sure if this is good idea to handle your source code in this way, since it could destroy your codes, however for your requirement, this line do the two substitution you wanted:
%s/\v(\s|^)\ze[a-z_]/&$/g|%s/\./->/g
at least it works for the example you gave.
Though you can build such "conditional remappings" with :imap <expr>, I highly recommend against doing that.
Your stated rules are still way too simplistic; the first one wouldn't allow you to enter any comments like Just a hack. (would turn into Just a hack->). You'll spend more time (laboriously) tweaking your rules, or undoing / suppressing the automatic conversion, just for saving a single keystroke here and there.
Imagine yourself in another editor, debugger, a colleague's laptop. Now you're dependent on your auto-conversion scheme, and you'll look like a fool!
For me, it looks like you're coming from a different knowledge and still feel the beginner's pain of walking in unknown terrain. Persevere; you'll get over that soon!

PHP: Converting scandic characters to hex unicode

I'm working on a Joomla site with Fabrik and problem is that Fabrik serializes some data using json_encode() but does not take into account the possibility of åäö and such. Now when a database search is made, it tries to find stuff with åäö, but doesn't find anything, because
everything is \u00e4 and \u00f6
and so forth.
I'm not much for digging into Fabrik's code and inserting one flag somewhere and worry about accidentally overwriting it when I update Fabrik. So I figured, since I'm disappointed in Fabrik anyway, I could just write around it completely in a custom template. Easy.
The problem is that I can't find a way or a function like htmlentities() that I can just feed the stuff to to make it match. I could just character replace them, but that's not a good solution.
Paraphrase: I wanna make word Mörkö into -> M\u00f6rk\u00f6. How?
Maybe there's another way but that works as excepted :
$encoded = substr(json_encode('Mörkö'), 1, -1);
json_encode('Mörkö') => "M\u00f6rk\u00f6"
substr() => M\u00f6rk\u00f6

Eclipse PHP would sometime enter tabs on arrays despite settings

So for eclipse PHP I set the tab policy to spaces with indentation size 3 from window->preferences->PHP-> Code Style -> Formatter
normally it enters 3 spaces instead of tab normally
but sometimes when I'm inside an array
$php = array(
);
and then I press tab, it would instead enter a tab character...
Is there a way to resolve this so that it always displays 3 spaces?

Actual input contents are not preseving on most of the browsers [FF,MSIE7/8 and etc]

I'm working on one application ( using PHP, javascript ). Below is the short description about my problem statement
There are two forms avaliable on my application, i.e. SourceFrm and targetFrm.
I am taking input on first form i.e. SourceFrm and doing processing on targetFrm.
Below is the input which I am taking from SourceFrm :
1) Enter your data (Identification of this input box id is 'inputdata' ):
2) Enter id ( Identification input box id is id ):
As per above input feed by user I am posting this data to targetFrm for further processiong.
On TargetFrm :
I am simply assigning inputdata value to php varible.
The spaces which are in between of words are getting lost ( more than one spaces converting to one space).
e.g.
User has added below data on input box and submitted
inputdata:
This is my test.
Here observed that user has added 5 spaces in between 'my' and 'test' word.
After assigning this input data to php variable. After that I printed this value
Below content I am getting
Output:
This is my test.
More than one spaces is converting to one space. This behaviour I checked on all browsers like FF,MSIE7/8 opera, safari, chrome.
If have used '<pre>' before printing php variable i.e.:
print "<pre>";
print $inputdata;
At time spaces are not getting lost (I am getting exact content).
Here my conflict is how do I presrve exact contents without using '<pre>'.
I have used encoding/decoding (htmlentitiesencode() and decode () )functionality, in my further data processing, so it may create some conflict if i replace spaces with . ( May conflict ll occur if i use instead space ).
Is anyone has any ideas, suggestions please suggest.
-Thanks
When you output your variables to HTML, they are parsed as HTML. Any additional white space is brought down to one space.
A simple fix would to replace all spaces with the html entitity to force browsers to display each space.
I wouldn't store the string with all the &nbps; in the database, but when you show it the would ensure that each space is seen.
EDIT
I mean only replace spaces on render...like:
print str_replace(' ', ' ', $inputdata);
HTML is capable of showing only one space. I'm not really sure why, but if you check your source code of rendered webpage containing your string, you'll see that it contains all the space, the browser just doesn't show it.
The same is for other space characters, as tabs.
The way to deal with it depends on type of your content. You can either replace spaces with or leave it as it is or do something completely different, i.e. strip more than one space down to one space.
It really depends on naturel of your data–the only real situation, when you would need more spaces than one, that comes to my mind is if you're trying to indent things with spaces, what actually isn't that great idea.
Edit: older resource:
http://www.sightspecific.com/~mosh/WWW_FAQ/nbsp.html

Why do PHP Array Examples Leave a Trailing Comma?

I have seen examples like the following:
$data = array(
'username' => $user->getUsername(),
'userpass' => $user->getPassword(),
'email' => $user->getEmail(),
);
However, in practice I have always not left the trailing comma. Am I doing something wrong, or is this just 'another' way of doing it? If I was using a framework would not having the trailing comma affect code generation negatively? I have seen the use of trailing commas in array declarations in other languages (Java, C++) as well, so I assume the reasons for leaving trailing commas are not specific to PHP, but this has piqued my interest.
Why do PHP Array Examples Leave a Trailing Comma?
Because they can. :) The PHP Manual entry for array states:
Having a trailing comma after the last defined array entry, while unusual, is a valid syntax.
Seriously, this is entirely for convenience so you can easily add another element to the array without having to first add the trailing comma to the last entry.
Speaking of other languages: Be careful with this in JavaScript. Some older browsers will throw an error, though newer ones generally allow it.
This is a good practice when defining array on multiple lines. It's also encouraged by ZendFramework's coding standards:
When using this latter declaration, we
encourage using a trailing comma for
the last item in the array; this
minimizes the impact of adding new
items on successive lines, and helps
to ensure no parse errors occur due to
a missing comma.
I noticed when working with version control (git) that if we add 1 thing to an array and we don't have the trailing comma, it will look like we modified 2 lines because the comma had to be added to the previous line. I find this looks bad and can be misleading when looking at the file changes, and for this reason I think a trailing comma is a good thing.
Because it keeps entries uniform.
If you've had to swap the order, or add or delete entries, you know being able to leave a trailing comma is very convenient.
If the last element cannot have a comma, then you end up having to maintain the last comma by modifying entries. It's a pointless exercise and a waste of time and finger strokes because the intent of swapping or modifying entries is already accomplished.
By allowing a trailing comma on the last element, it frees the programmer from having to tend to this annoying and fruitless detail.
The reason is commit changes.
If you have to add the trailing comma when adding a new element. You're changing 1 line and adding 1 line. (-++)
When adding a new element when a comma is already in the line above. There is only 1 added line, and no changed ones. (+)
I can't speak for other people, but I usually leave a trailing comma in my code. I do so because if/when I later add to the array, I do not have to worry about missing out a comma due to forgetting to add a comma to what was previously the last line.
I'm always doing trailing comma because it helps to avoid syntax errors while adding new array elements... it's just a good practice.
I feel that even though it is allowed it is bad practice, its like leaving out the last semi colon of your functions and loops.
If you look at an example of roundcube file config (config.inc.php), they have example with and without trailing comma.
This array defines what plugins should be enabled or disabled:
...
// List of active plugins (in plugins/ directory)
$config['plugins'] = array(
'managesieve',
'password',
'archive',
'zipdownload',
);
...
Normally, this would be line by line and if somebody wants to add something on the array, they can do this:
...
// List of active plugins (in plugins/ directory)
$config['plugins'] = array(
'managesieve', //code by personA
'password', //code by personA
'archive', //code by personA
'zipdownload', //code by personA
'newplugin', //new code by personB
);
...
So, when they commit this code, they see only one changes for that particular line and this is more readable when inspecting who is making the code changes for that particular line.
In another line of code you can see this without trailing comma:
...
$config['default_folders'] = array('INBOX', 'Drafts', 'Sent', 'INBOX.spam', 'Trash');
...
Normally it would be a single line of code where nobody expects this code to be changed frequently.
In another word:
1) Put trailing comma if the array is used as an option or configuration file that might need to be changed dynamically in the future. Besides, if you make changes to that array programmatically using trailing comma you only make changes to one line code, whereas without it, you have to deal with 2 line of codes and this can cause more complexity to parse the array
2) You don't have to put trailing comma if the array is a constant array and you don't expect it to change in the future but as mentioned by the Accepted Answer, you can put trailing comma but it has no purpose
This surprised me recently, but it makes sense. I have long tried to adhere to an earlier convention that accomplishes the same thing, which is to put the separating comma in front of each entry rather than at the end.
$data = array(
'username' => $user->getUsername()
, 'userpass' => $user->getPassword()
, 'email' => $user->getEmail()
);
The commas also all line up that way, which looks nice, but it can make the indenting a little awkward. Maybe for that reason, it doesn't seem to have caught on much over the years, and I've had others ask me why I do it. I guess PHP's solution is a good compromise, and in any case, it's apparently the accepted solution now.
I've always added commas at the start of the new entry.
Compilers see it as the single-character token of look-ahead
that says "there is another one coming". I don't know if modern
compilers use LR(1) (left-recursive, single token look-ahead) but
I think that's where the syntax error originates when an a comma
has nothing after it.
It is rare that I've ever had another developer agree with me, but
it looks like JohnBrooking does!

Categories