PHP order by length but ignore characters - php

I am changing a php plugin and having a hard time to get the sorting of the array it pulls from the database right. As of now I have it like:
order by LENGTH(name), name
Which gets me close but not right. It outputs like so:
8.2-11
8.2-12
8.2-13
8.2-14
8.2-13-A
Which I understand because 8.2-13-A is longer than 8.2-13 so it will be at the end. How can I ignore the -A or -B so the two 8.2-13's are right after each other?

You could try to find the last occurance of the - sign by using something like:
LENGTH(name) - LOCATE("-", REVERSE(name))
With this information it should be possible to only use a substring of the original string like:
ORDER BY LENGTH(SUBSTRING(name, (LENGTH(name) - LOCATE("-", REVERSE(name))))), SUBSTRING(name, (LENGTH(name) - LOCATE("-", REVERSE(name))))
BUT: I'm pretty sure this could have severe influence on the performance.
Maybe it's a better idea to implement a solution with a stored procedure or do this directly in php.

Related

How to resolve this new bug/change in behaviour in PHP 8.x in regards to NumberFormatter?

I thought I was going crazy before I went out of my way to restore PHP 7.4.13 to try it to make sure. It has changed in PHP 8.x!
Code:
$a = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
$a->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 8);
$a->setSymbol(NumberFormatter::CURRENCY_SYMBOL, ''); // It's empty because I want it "minimal" for a table.
var_dump($a->formatCurrency(0.0043, 'BTC'));
Output in PHP 7.4.13:
string(6) "0.0043"
Output in PHP 8.0.1:
string(11) "BTC 0.0043"
Conclusion:
Something has changed.
Question:
How to make PHP 8.0.1 behave like it used to?
Clarification:
That is, to make the money sum outputted in a "minimal" manner, with just the number.
Prediction 1:
Somebody might respond something like: "Just output the number manually for these cases!"
To this, I respond: It has to be the number formatter in currency mode. There are wild differences between locales/languages in how money sums are to be formatted. I need to use this and tell it to forgo the symbol.
Prediction 2:
Somebody might respond something like: "Just str_replace/preg_replace out the symbol! Easy!"
To this, I respond: Not all locales/languages use the same format. It's a whole science of numerous different formats, impossible to account for with a simple string replacement.
I want to do this right, which is what I thought I had. Until just now, when PHP 8 has decided to change this around. I'm not sure if it's a bug or what.
Research:
I cannot find any mention whatsoever of PHP 8 here: https://www.php.net/manual/en/numberformatter.formatcurrency.php

Vim: sort al use statements inside a php file

I'll like to learn how to select all lines that starts with use, inside current php file, and then sort them. Until now: I've worked with :sort command. Also, ... I've remapped sort command:
map <Leader>s :sort<CR>
But I'll like to create a complex function that select all use statements inside a php file, and after that, that sort all that lines.
I know how to sort. But I have no idea of what is the way to select some arbitrary group of strings with VimL
function! SortUseStatements()
let g:select='/^use .*$<CR>V' !?!?!?!?!?!?!?!?!?
exec g:select
endfunction
map <F2> :call SortUseStatements()<CR>
I know that I want to execute commands like:
go to the beginning of current file
sarch first ^use .*;$ statement
start selection
go to the end of current file
sarch last ^use .*;$ statement
and finally, run :sort
This can be neatly done with :help cmdline-ranges.
You want the search to start at the beginning of the file, so your initial address is 0 (to start before the first line). Then, search for the first line beginning with use. When separated with ; the cursor position will be set to that line before interpreting the next line specifier. Then, you need to navigate to the last of the use statements (starting from the first use line, so again separated by ;). I first choose an easy way that searches for an empty line, and then takes the previous one. With :print, you can check that the range is fine before replacing the command with :sort:
:0;/^use /;/^$/-1 print
If there's no empty line after the use block, you have to use a more complex pattern for any line not starting with use:
:0;/^use /;/^\(use \)\#!/-1 print
Alternatives
For anything more complex, a single :range probably won't do. I would then determine the start and end line numbers separately, using let lnum = search(...) with appropriate patterns. Then you can insert the numbers into an Ex command via :execute startLnum . ',' . endLnum . 'sort'

How to easily generate debugging statements for PHP code?

I need to be able to generate debugging statements for my code. For example, here is some code I have:
$this->R->radius_ft = $this->TC->diameter / 24;
$this->R->TBETA2_rad = $this->D->beta2 / $rad; //Outer angle
$this->R->TBETA1_rad = $this->R->inner_beta1 / $rad; //Inner angle
I need to be able display results of computations so that they can be read by a human.
So far I have been doing this (example showing first line from above only):
$this->R->radius_ft = $this->TC->diameter / 24;
if (self::DEBUG)
print("radius_ft({$this->R->radius_ft}) = diameter({$this->TC->diameter}) / 24");
The above print something like radius_ft(1.4583) = diameter(35) / 24 and a few of those lines looks like equations and are nicely traceable when I want to verify things on paper, or if I want to expose the intermediate work of the computations to someone else.
The problem is that it is a pain to construct those debugging statements. I craft them by hand, and usually it is not a problem, but in my current example, there are hundreds of lines of code where this needs to be done. Much pain.
I was wondering if there are facilities in PHP that will allow me to make print-outs of statements showing what each line of code does. Or methods to semi-automate creating the debug lines for me.
I have so far discovered this method to cut down on some of the work .... use Macro facilities of a text editor.
Paste line of code into TextPad (or similar editor that supports macros). Record macro and use Search, Mark and Copy facilities to carefully navigate between special symbols of the variable, such as $, >, and symbols that are not alphanumeric or $, >, etc. while copying and extracting and pasting parts of variable to craft my particular statement.
Exact steps may differ for one's needs. My macro operates on one variable like $this->R->radius_ft with cursor at the start and ends up with something like radius_ft({$this->R->radius_ft}), with cursor a few chars after the end, sometimes coinciding with the next variable to process.
Perhaps same could be done with regular expressions but I like the way Macro does it - I can process a variable and go to the next one and just repeat the macro with a hot key combination. This takes out the most tedious chunk of work for me.
Alternatively - hand the person the code and let them figure it out. Teach them how to read code.

PHP: Use a $_GET-Param with multiple other Params within a $_GET-Param

yeah, I know, the title is kind of confusing, but no better title came to my mind.
Here is my problem:
I want to use a link in my application, which would look like this:
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
The problem is that &someparam2 is meant to hang on the second $_GET-Param.
It would be like this:
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
Instead, PHP interprets that &someparam2 hangs on the first $_GET-Param.
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
Does anyone know a solution for this?
I already tried
localhost/index?jumpto='some_folder/somescript.php?someparam1=1234&someparam2=4321'
but of course that didn't work.
I hope you can understand my problem.
Thank you for your time.
You will need to URL encode your string some_folder/somescript.php?someparam1=1234 so that php will not parse & in the query string as a param separator.
use urlencode("some_folder/somescript.php?someparam1=1234");

Setting language for user

I would like my php website to be able to be multilinguistic. I thought of using:
echo $lang[$_SESSION['lang']]['WellcomeMessage'];
but I found that I will be needing to format the text, say for example male/female or putting some values from the DB. So I thought that simple strings might not do the trick for formatting?
I know #define might have worked in C as the string translates to code, but I don't know how php does that. For example:
define ($lang['en']['credit_left'],'you have $credits_left');
define ($lang['sp']['credit_left'],'tienes $credits_left creditos mas');
Any suggestions?

Categories