Utf8 Sort Array - php

i have some problems with sorting an array.
List
0 => string 'Australien' (length=10)
1 => string 'Belgien' (length=7)
2 => string 'Botswana' (length=8)
3 => string 'Brasilien' (length=9)
4 => string 'Bulgarien' (length=9)
5 => string 'Burma' (length=5)
6 => string 'China' (length=5)
7 => string 'Costa Rica' (length=10)
73 => string 'Ägypten' (length=8)
But Ägypten should be after Australien.
I already tried with the Collator class but our client wont install the extension.

You can use setlocale along with first parameter LC_COLLATE and second locale with en_US.utf8 and simply sort using usort along with strcoll try as
setlocale(LC_COLLATE, 'en_US.utf8');
$array = array('Australien','Belgien','Botswana','Brasilien','Bulgarien','Burma','China','Costa Rica','Ägypten');
usort($array, 'strcoll');
print_r($array);
Demo

Related

Can not match the last group of numbers using php preg_match()

preg_match_all("/(\d{12})
(?:,|$)/","111762396541,561572500056,561729950637,561135281443",$matches);
var_dump($mathes):
array (size=2)
0 =>
array (size=4)
0 => string '561762396543,' (length=13)
1 => string '561572500056,' (length=13)
2 => string '561729950637,' (length=13)
3 => string '561135281443' (length=12)
1 =>
array (size=4)
0 => string '561762396543' (length=12)
1 => string '561572500056' (length=12)
2 => string '561729950637' (length=12)
3 => string '561135281443' (length=12)
But I want the $matches like this:
array (size=4)
0 => string '561762396543,' (length=13)
1 => string '561572500056,' (length=13)
2 => string '561729950637,' (length=13)
3 => string '561135281443' (length=12)
I wanna match groups of numbers(each has 12 digits) and a suffix comma if there is one.The exeption is the last group of numbers,it doesnt have to match a comma,cause it reaches the end of the line.
Try this instead:
preg_match_all("/(\d{12}(?:,|$))/","111762396541,561572500056,561729950637,561135281443",$matches);
When the $ is inside your character range brackets [ ] it is looking for the $ characters not the end-of-line.
EDIT: If you want to include the comma in your matches, then just use the above code sample and look at $matches[0].
If you wanted an easier syntax that matches any sort of word boundary, the \b will match commas and end-of-line, too:
preg_match_all("/(\d{12}\b)/","111762396541,561572500056,561729950637,561135281443",$matches);

JpGraph : no error but broken picture

I make a graph with JPGraph but I've a little problem.
No error was displayed but the picture is a "broken picture".
Here is my code :
$graph = new Graph(1000,300);
$graph->img->SetMargin(40,30,50,40);
$graph->SetScale("textlin");
$graph->title->Set("Graph");
$graph->ygrid->Show();
$graph->ygrid->SetColor('blue#0.7');
$graph->ygrid->SetLineStyle('dashed');
$graph->xgrid->Show();
$graph->xgrid->SetColor('red#0.7');
$graph->xgrid->SetLineStyle('dashed');
$graph->title->SetFont(FF_ARIAL,FS_BOLD,11);
$bigCourbe = array();
$i = 0;
foreach($bigData as $data)
{
var_dump($data);
$courbe = new LinePlot($data);
$courbe->value->Show();
$courbe->value->SetFont(FF_ARIAL,FS_NORMAL,9);
$courbe->value->SetFormat('%d');
$courbe->mark->SetType(MARK_FILLEDCIRCLE);
$courbe->mark->SetFillColor("green");
$courbe->mark->SetWidth(2);
$courbe->SetWeight(10);
echo $function[$i];
$courbe->SetLegend($function[$i++]);
$bigCourbe[] = $courbe;
}
$graph->xaxis->title->Set("Heures");
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
foreach ($bigCourbe as $elem) {
$graph->Add($elem);
}
//$graph->Stroke();
So, the result seems good :
array (size=24)
0 => string '0.000' (length=5)
1 => string '0.000' (length=5)
2 => string '0.000' (length=5)
3 => string '0.000' (length=5)
4 => string '0.000' (length=5)
5 => string '0.000' (length=5)
....
Index HC <- it's the $function[$i] var
array (size=24)
0 => string '0.200' (length=5)
1 => string '0.200' (length=5)
2 => string '0.100' (length=5)
3 => string '0.200' (length=5)
4 => string '0.200' (length=5)
5 => string '0.200' (length=5)
....
Index HP
array (size=24)
0 => string '0.000' (length=5)
1 => string '0.000' (length=5)
2 => string '0.000' (length=5)
3 => string '0.000' (length=5)
4 => string '0.000' (length=5)
5 => string '0.000' (length=5)
Index HPTE
array (size=24)
0 => string '0.200' (length=5)
1 => string '0.200' (length=5)
2 => string '0.100' (length=5)
3 => string '0.200' (length=5)
4 => string '0.200' (length=5)
5 => string '0.200' (length=5)
....
Total Heure
I do not see the problem...
If I remove the debug, and uncomment the $graph->Stroke(); line. I have that :
Can you help me ?
Thank you in advance.
in my case (php 7.1.0) it was Deprecated message
just put
ini_set('display_errors', 0);
at the top of your script and try again.
or
comment out $graph->Stroke(); and see what error(or warning) you have
I know this is an old question but I just had the same problem and fixed it by removing everything before the <?php.
THis was suggested in the link below - check 1.b.
Solution Source
How do you display the graph? Do you have an HTML page? Please show the HTML code.
I have a similar problem where I have no error message, but get the broken image icon.
jpGraph can't include PHP file
One thing I would recommend is to add the lines
//save to file
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);
if the image from the file is correct, then you can rule out the graph generating code.

What array function should I use for creating an index?

Hello guys I am trying to create an index of all words on html page that my crawler parses.
At this moment I have managed to breakdown the html page into an array of words and I have filtered out all the stop words.
At this stage I have a few problems.
The array of words from the parsed html page have words that are repeated, I like that because I still have to record how many times a word appeared in the page.
The array looks like this.
$wordsFromHTML =
array (size=119)
0 => string 'web' (length=3)
1 => string 'giants' (length=6)
2 => string 'vryheid' (length=7)
3 => string 'news' (length=4)
4 => string 'access' (length=6)
5 => string 'mails' (length=5)
6 => string 'mobile' (length=6)
7 => string 'february' (length=8)
8 => string 'access' (length=6)
9 => string 'mails' (length=5)
10 => string 'web' (length=3)
11 => string 'february' (length=8)
12 => string 'access' (length=6)
13 => string 'mails' (length=5)
14 => string 'desktop' (length=7)
15 => string 'february' (length=8)
16 => string 'hosting' (length=7)
17 => string 'web' (length=3)
18 => string 'giants' (length=6)
19 => string 'vryheid' (length=7)
20 => string 'february' (length=8)
22 => string 'us' (length=2)
Now I want to save all the words from the $wordsFromHTML to the $indesArray which is my final index.
It should look like this.
$indexArray = array('web'=>array('url'=>array(0,10,17)))
The problem is how to keep incrementing the position ($wordsFromHTML keys) for each word that was repeated from the $wordsFromHTML array in the final index array.
The index array should only have unique words and if another word that already exists try to come in, we use the already existing word which has the same URL and increment its position.
Hope you understand my question.

Smarty value of array extraction using date_format result on other array value as a key

I have two arrays in a smarty template: $months and $contract.
{$months|var_dump} gets this:
array (size=12)
1 => string 'января' (length=12)
2 => string 'февраля' (length=14)
3 => string 'марта' (length=10)
4 => string 'апреля' (length=12)
5 => string 'мая' (length=6)
6 => string 'июня' (length=8)
7 => string 'июля' (length=8)
8 => string 'августа' (length=14)
9 => string 'сентября' (length=16)
10 => string 'октября' (length=14)
11 => string 'ноября' (length=12)
12 => string 'декабря' (length=14)
array values are russian names of months in genitive.
{$contract|var_dump} gets this
'date_till' => '1355518365' (length=10)
so I need to create a month number first from $contract.date_till. it is usually done like
{$contract.date_till|date_format:"%m"}
And now the question is: how do I extract a month name from $months array by the month number made of $contract.date_till with date_format?
I've tried many variants described in smarty manuals, but noone works. For example, this one doesn't:
{$months[{$contract.date_till|date_format:"%m"}]}
{assign var=monthNo value=$contract.date_till|date_format:"%m"}
{$months.$monthNo}
This will give u the month of the necessary date.

PHP range for hebrew alphabets

PHP has a function range('a','z') which prints the English alphabet a, b, c, d, etc.
Is there a similar function for hebrew alphabets?
You can do something like this:
function utfOrd($c) {
return intval(array_pop(unpack('H*', $c)),16);
}
function utfChr($c) {
return pack('H*', base_convert("$c", 10, 16));
}
var_dump(array_map('utfChr', range(utfOrd('א'), utfOrd('ת'))));
Prints:
array
0 => string 'א' (length=2)
1 => string 'ב' (length=2)
2 => string 'ג' (length=2)
3 => string 'ד' (length=2)
4 => string 'ה' (length=2)
5 => string 'ו' (length=2)
6 => string 'ז' (length=2)
7 => string 'ח' (length=2)
8 => string 'ט' (length=2)
9 => string 'י' (length=2)
10 => string 'ך' (length=2)
11 => string 'כ' (length=2)
12 => string 'ל' (length=2)
13 => string 'ם' (length=2)
14 => string 'מ' (length=2)
15 => string 'ן' (length=2)
16 => string 'נ' (length=2)
17 => string 'ס' (length=2)
18 => string 'ע' (length=2)
19 => string 'ף' (length=2)
20 => string 'פ' (length=2)
21 => string 'ץ' (length=2)
22 => string 'צ' (length=2)
23 => string 'ק' (length=2)
24 => string 'ר' (length=2)
25 => string 'ש' (length=2)
26 => string 'ת' (length=2)
If you need some more characters, you can use this to create your hardcoded array or merge few ranges.
Range can work with the standard western alphabet because the characters A thru Z are consecutive values in the ASCII (and UTF-8) character set.
Hebrew characters are not ASCII chars (see this list) but you could set an initial range of the UTF-8 numeric values and then just array_map that to characters.

Categories