Eclipse PHP would sometime enter tabs on arrays despite settings - php

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?

Related

PHP: remove BOM at the very beginning of the file

I'm using AMP with PHP and when I'm trying the AMP validator (ttps://validator.ampproject.org/) I get a couple of errors that do not make sense become of a BOM character at the beginning of my page.
Example:
URL: https://www.laurentwillen.be/gadgets/xiaomi-redmi-note-7-test-avis-et-prix/
I get the following error: (among others)
The mandatory attribute '⚡' is missing in tag 'html'
If you look at the source of this page, the attribute is there but when you use the AMP validator, there is a red dot appearing at the very beginning of my page.
If I add a letter at the very top of my code like this:
A
<?PHP
// my code goes here
?>
I see the red dot appearing before the letter which highlights that my code is not generating the issue. In Notepad++ I see that the encoding is set to UTF8.
Do you know how I could remove this so that I can work on the real AMP errors?
Thanks
Laurent

A small issue with PhP on Mac's text editor

I am editing my PhP files with Text Edit on my Mac.
The PhP files are source code I got from some tutorial.
The subtle issue I was able to notice is that the apostrophe ' on the source file is not the same apostrophe I type with my keyboard.
Also, when I try to type inside an existing string with the "source apostrophe" I notice the last letter goes outside the string and I am sure this is not a typo from my side.
When I put my keyboard apostrophe in the PhP file I get a 500 internal server error when requesting the PhP file.
If I just copy paste a "source apostrophe" I don't get the internal error.
Any idea what is going on here?
EDIT: As funny as it may be, I have put the 3 apostrophe here. The first one is from the source code(most left) the other two are from my keyboard.
' ' `
I just had a similar problem and discovered it was the Mac text editor automatically substituting Smart Quotes for single quotes. In TextEdit this can be disabled for the current document by unchecking the menu item Edit->Substitutions->Smart Quotes. Or uncheck Smart Quotes in the TextEdit Preferences->New Document->Options for all new documents.
Ok, I figured this out.
The apostrophe I was getting from my keyboard in Text Edit was UTF 8, it was 3 bytes long.
The "good" apostrophe was simply 27 hex.
I am now editing my PhP files with xCode instead of Text Edit.
I am guessing text edit does some rich text editing? Not sure.
This problem is related to MacOS System.
Solution:
System Preferences -> Keyboard -> Text Tab
for singled quotes: 'abc'
for doubled quotes: "abcd"
there, you can change the selected value.

Change indentation space for PHPs "\t" [duplicate]

This question already has an answer here:
Can I change tab width (\t) in a PHP string?
(1 answer)
Closed 9 years ago.
Is it possible to change the indentation space for "\t" in PHP? Searched for a while now but can't find anything pointing to an answer. I know there are some libraries out there that can tidy up the output but I'd prefer to be able to set this anyway, and to keep it consistant with my HTML indentation. I wish to change this indentation so that it becomes 4 spaces instead of the default "tab" if possible.
« The tab width is not specified by PHP, but by the program which views it. If you view it in an editor, you can set the tab width in the editor. »
Can I change tab width (\t) in a PHP string?
Specifying Tab-Width?
A tab character is a tab character. It doesn't have a width, it's a tab character. The thing (editor, viewer, browser) that is rendering the tab character visually (by indenting content) decides the width. There's no PHP or other "setting".
If you want tabs from a text input to become 4 spaces you can do this:
$formatted_string = preg_replace("/\t/", " ", $string);
For this to work on a html page you might want to replace the spaces with four

Converting entire source code from tabs to 4 spaces

I just realized after committing the CakePHP source to GitHub that they're now using tabs to indent code rather than four spaces. They also define this in the .editorconfig file, which I've changed to this:
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
Is there a way to run through the entire source code and safely convert all tabs to four spaces for indentation? My reasoning is every developer on the repo uses four spaces and mixing and matching will cause the code to look out of place when looking at it on GitHub. And I'm just a fan of consistency :)
If I'm going down the home-brew way and writing my own script for this, I don't really mind what language although I'm more confident in PHP (not the best suited for the job, I know). Is this as simple as doing a preg_replace('~\t~', ' ', $fileText) on each file?
Try this in the directory you wish to execute it in:
find ./ -type f -exec sed -i 's/\t/XXXX/g' {} \;
That should replace the tabs with 4 spaces (if you replace the X's with spaces).
Adjust the space between t/ and /g with however many spaces you want…just get rid of the X's and put spaces in there.
A straight replacement of tabs with spaces will result in misalignment when tabs follow space characters that encroach on that tab region.
A basic python script which makes use of the expandtabs() string method will result in code looking the same as when it was conceived. Example is for a tab space of 4:
#!/usr/bin/python
#
# convert source code or text with spaces, being careful to align text as it was conceived
# with the original tab space settings, which is defaulted to 4 spaces per tab.
#
# usage:
# ./tabs2spaces.py <file_to_convert>
import os
import sys
spaces_per_tab = 4
argc = len( sys.argv )
if argc < 2:
print 'no file argument specified'
filename = sys.argv[ 1 ]
old_filename = 'old_' + filename
os.rename( filename, old_filename )
fn = open( filename, 'wb' )
fo = open( old_filename, 'r' )
for line in fo:
fn.write( line.expandtabs( spaces_per_tab ) )
fn.close()
fo.close()
Not sure if you have access to or already use Sublime Text 2, but it can automatically convert all the tabs to spaces for you:
How to replace four spaces with a tab in Sublime Text 2?

Pushing to github - strange white spaces

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

Categories