i'm trying to put input (below) into multiple arrays (or maybe its simpler into one array) to finally export it into mysql table, input data goes like:
0 98
77 09
0 12
0 98234
32 0
0 1
0 0
345 32
34 9
6437 34
789 0
0 0
.
.
34 0
my simple code ($run_txt_filter1 is input):
if ( $counted == 64)
{
echo "line's number: ".$counted;
//echo $run_txt_filter1;
for ($modi = 0; $modi <= 15; $modi++)
{
for ($simhi = 1; $simhi <= 4 ; $simhi++)
{
$exploded=explode(" ", $run_txt_filter1);
var_dump($exploded)." \n";
}
}
}
Why var_dump keeps saying the id from 0-64 ? (there always should be 64 input lines).
What a really want to achieve is:
array0=(0, 77, 0, 0)
array1=(98, 09, 12, 98234)
array2=(32, 0, 0, 345)
.
.
array30=(0, 12, 0, 34)
array31=(0, 0, 0, 0)
thanks in advance
try to separate your explode for the next line and explode for the white space. cause on your code above it's reading the whole string as one causing your program to store it on a single array. so it would be a nested loop by then. :)
Considering this input:
$in = <<<IN
0 98
77 09
0 12
0 98234
32 0
0 1
0 0
345 32
34 9
6437 34
789 0
0 0
IN;
This algorithm solves your problem i think:
$final_array = array();
$offset = 0;
foreach(preg_split("/(\r?\n){2,}/",$in) as $block){
foreach(preg_split("/(\r?\n)/",$block) as $line){
foreach(preg_split("/\s+/",$line) as $column => $value){
if($value=='') continue;
if(!isset($final_array[$offset+$column]))
$final_array[$offset+$column] = array();
$final_array[$offset+$column][]=$value;
}
}
$offset = count($final_array);
}
print_r($final_array);
try something similar to this one I'm not sure if this would work:
inputs:
$inputs = "0 98
77 09
0 12
0 98234
32 0
0 1
0 0
345 32
34 9
6437 34
789 0
0 0
.
.
34 0";
code:
$run_txt_filter1 = explode("\n", $inputs);
if(count($run_txt_filter1) == 64)
{
foreach($run_txt_filter1 as $input)
{
$exploded = explode(' ', $input);
print_r($exploded);
}
}
The "\n" is the next line for windows but it's different on linux its "\r" I'm not sure if it would work you may also try the combination as
explode("\r\n", $inputs);
but just a try if the next line won't work I think you could use some other way to separate each set of values either user other type of characters like ',',';',':' etc. :)
Related
I want to display the character codes of a string in php.
<?php
$Text = '{"text":"سلام"}';
for($i = 0;$i < strlen($Text);$i++){
echo mb_ord($Text[$i]),"<br>";
}
?>
But its output is null
You are trying to get all "bytes" of the unicode string. You need to use mb_strlen() to get the real length of the characters.
Then, you need to use mb_substr() to get the right character.
$Text = '{"text":"سلام"}';
for($i = 0; $i < mb_strlen($Text); $i++) {
echo mb_ord(mb_substr($Text,$i, 1)), "<br>";
}
Output :
123
34
116
101
120
116
34
58
34
1587
1604
1575
1605
34
125
See also all mb_* functions
<?php
tree($Comments, 0, 0);
$var = -1;
function tree($Comments, $parent_id = 0, $level=0) {
global $var;
foreach($Comments as $Comment){
if($Comment['parent_id'] == $parent_id) {
If ($level > $var) {$var++;} else {echo ($var-$level+1); $var=$level;};
for ($i=$level; $i>0; $i--)
echo "-";
echo $Comment['text']."<br>";
tree($Comments, $Comment['id'], $level+1);
}
}
}
?>
This is my recursive method of displaying the tree of comments(thread comments),
the problem is I have to make, in that (if ($level > $var) statement get variable $var of how many (</div> for a bootstrap design) I should use for making the design for the comment threads. The problem is that I get first result 1, which supposedly I shouldn't, the rest gets as It should have to.
Edit 1, Example of data in database(to easy understand):
id id_theme parent_id user text
16 14 0 DomainFlag This is 1!
17 14 16 DomainFlag This is 2!
18 14 16 DomainFlag This is 3!
20 14 17 DomainFlag This is 4!
21 14 17 DomainFlag This is 5!
22 14 21 DomainFlag This is 7!
23 14 22 DomainFlag This is 8!
24 14 18 DomainFlag This is 6!
25 14 0 DomainFlag This is 9!
26 14 25 DomainFlag This is 10!
27 14 25 DomainFlag This is 11!
My table:
id
parent_id
id_theme
user
text
upVotes
downVotes
My idea is to make a threaded system of comments like(with $level which I get from the recursive function):
0
1
2
3
2
1
1
0
1
0
So I get this result with that 1 at first no matter what, which I shouldn't( 1 This is 1!). The rest is working perfectly.
1This is 1!
-This is 2!
--This is 4!
1--This is 5!
---This is 7!
----This is 8!
4-This is 3!
--This is 6!
3This is 9!
-This is 10!
1-This is 11!
Why that 1 at the beginning appears? I heard that global scopes isn't good to implement, why? and what I should do to not use it?
I have this array $awstat that I extacted from an awstat file, an withing $awstat I have this that I need:
BEGIN_TIME 24
0 3245 9955 143463426 8047 13601 475741423
1 3122 9131 146244440 7579 12936 507921700
2 2639 5706 95369716 7351 11987 490330698
3 1917 4062 79234871 8245 13009 579453498
4 1757 4263 65580607 7887 11437 454870321
5 1723 4022 44682383 6888 10263 326819624
6 1876 4677 56964771 7339 11242 355385677
7 2796 8473 120152521 7770 12176 362904239
8 4227 13791 196173677 7421 12196 366706352
9 7984 25965 375376297 8398 13883 406545549
10 14605 34418 434054375 7183 13341 380773129
11 15533 41259 559938996 7123 12690 372426426
12 17495 40043 505139834 7432 13402 518541077
13 15815 34170 385108531 6519 12390 396494926
14 16330 41073 508838859 6761 12318 348417806
15 19093 44058 483568307 7692 13583 454365520
16 30429 59672 577852398 8273 13231 473134295
17 25094 48897 478246556 8207 12898 476038603
18 19136 42665 482073005 8087 12983 468300958
19 28849 46228 371229572 7721 12688 471632281
20 14068 30981 341103557 7832 13251 417443822
21 14727 33458 394841797 7575 12644 388811384
22 13480 31364 365096742 7460 13114 411771572
23 7189 19744 272606100 6643 12398 397762547
END_TIME
So I tried this and it doesn't seem to work!
preg_match("/BEGIN_TIME(.*)END_TIME/is", $awstats, $matches);
$time = $matches[0] ;
var_dump($time); // it displays "NULL"
Any solution for this? Thanks!
Not so much an answer, more of an opinion.
Using regular expressions here is overkill.
Something as simple as this will do:
$lines = file("awstats.output");
$lines = array_slice(1); // remove first line
$lines = array_slice(0, -1); // remove last line
foreach ($lines as $line) {
$data = explode(" ", $line);
// handle data
}
You can match linebreaks with:
([^\n]*\n+)+
So this should work:
preg_match("/BEGIN_DAY([^\n]*\n+)+END_DAY/is", $awstats, $matches);
$time = file_get_contents('awstat.log');
$time = preg_replace('/BEGIN_TIME(.*?)END_TIME/sim', '$1', $time);
I need some regular expression to split PO (language translation file) file's translated count , fuzzy count and total string count.
I used PHP for the program, I search every where but couldn't found.
please help me.
gettext PO files are so old and ubiquitous, they're a de facto industry standard with great support by a wide variety of tools. Trying to reinvent a solution using regexen here seems very inappropriate when you could be using one of the many PO file parsers instead. For example oscarotero/Gettext:
$translations = Gettext\Extractors\Po::extract('messages.po');
$total = $translated = $fuzzy = 0;
foreach ($translations as $translation) {
$total++;
if (!$translation->hasTranslation()) {
$untranslated++;
}
if (in_array('fuzzy', $translation->getComments())) {
$fuzzy++;
}
}
(Untested, but should work immediately or with slight changes.)
In fact though, there are tools to do this already: Translate Toolkit or Pology, for those I know of:
$ pocount locale/ko/LC_MESSAGES/
data/locale/ko/LC_MESSAGES/messages.po
type strings words (source) words (translation)
translated: 3 ( 0%) 7 ( 0%) 28
fuzzy: 0 ( 0%) 0 ( 0%) n/a
untranslated: 729 ( 99%) 1065 ( 99%) n/a
Total: 732 1072 28
unreviewed: 3 ( 0%) 7 ( 0%) 28
empty: 729 ( 99%) 1065 ( 99%) 0
$ posieve stats locale/ko/
- msg msg/tot w-or w/tot-or w-tr ch-or ch-tr
translated 3 0.4% 15 0.9% 26 93 114
fuzzy 0 0.0% 0 0.0% 0 0 0
untranslated 729 99.6% 1708 99.1% 0 17323 0
total 732 - 1723 - 26 17416 114
obsolete 0 - 0 - 0 0 0
Try this regex,
$total = array();
$translated = array();
$extra ='';
// If fuzzy true then translated count = fuzzy count
if($fuzzy) {
$extra = '#, fuzzy\n';
}
$matched = preg_match_all('/'.$extra.'msgid\s+((?:".*(?<!\\\\)"\s*)+)\s+'.'msgstr\s+((?:".*(?<!\\\\)"\s*)+)/', $po_content, $matches);
for ($i = 0; $i < $matched; $i++) {
if(trim(substr(rtrim($matches[1][$i]), 1, -1))!="") {
$total[] = substr(rtrim($matches[1][$i]), 1, -1);
}
if(trim(substr(rtrim($matches[2][$i]), 1, -1))!="") {
if (strpos(substr(rtrim($matches[2][$i]), 1, -1), 'Language-Team')===false && strpos(substr(rtrim($matches[2][$i]), 1, -1), 'MIME-Version')===false ) {
$translated[] = substr(rtrim($matches[2][$i]), 1, -1);
}
}
}
Total count = count($total);
Translated count = count($translated);
I am to import a file, say june.txt that would have data such as the following data:
Sandy,820,384,133,18,408
Wanda,120,437,128,807,595
Jane,631,415,142,687,600
Andrea,179,339,349,594,986
Wanda,803,191,6,807,322
Jane,741,975,34,15,832
Jane,239,714,250,94,497
Andrea,219,188,411,584,713
And then the PHP would parse it into 2 difference ways:
The first way being all the names bundled together with totals, such as:
Sandy 820 384 133 18 408
Total 820 384 133 18 408
Jane 631 415 142 687 600
Jane 741 975 34 15 832
Jane 239 714 250 94 497
Total 1611 2104 426 796 497
Andrea 179 339 349 594 986
Andrea 219 188 411 584 713
Total 398 527 760 1178 1699
Wanda 120 437 128 807 595
Wanda 803 191 6 807 322
Total 923 628 134 1614 917
The second way would total and add the names together in a big list, such as
Sandy 820 384 133 18 408
Jane 1611 2104 426 796 497
Andrea 398 527 760 1178 1699
Wanda 923 628 134 1614 917
Any logic or suggestions would be helpful, I am new to PHP and not sure how this could even be done. My plan is to eventually display the results in HTML tables and have them sortable, but I can tackle that at a later date, Unless someone feels obligated to just add the and such for me in the parsing.
I think something useful for you would be the explode function.
As far as creating these views I'd start by loading all this data into an associative array of arrays based on the name, then iterate as necessary:
$datafile = file("filename.txt");
// reads lines into an associative array (key is the name) of arrays
// where each sub-array is a list of the records for each name
$arr = array();
foreach($datafile as $line){
$temp = explode(',', $line);
$arr[$temp[0]][] = $temp;
}
// iterate over each person
foreach($arr as $person_set){
// create an array to hold the sum of each column
// (and the name in the first column)
$totals = array();
$totals[0] = $person_set[0][0];
for($i = 1; $i < length($record); $i++){
$totals[$i] = 0;
}
// now iterate over each record for this person
foreach($person_set as $record){
// print a particular record
echo implode(' ', $record) . '<br>';
// add each column (1..end) to the totals array
for($i = 1; $i < length($record); $i++){
$totals[$i] += $record[$i];
}
}
// print out the totals line
echo implode(' ', $totals) . '<br><br>';
}
I'll leave formatting this data into a table as an exercise.
Well, to start, I'd real a file like that with PHP's fgetcsv(). Docs.
You could try a script like this:
<?php
echo "<table>";
$data = file("june.txt");
foreach($data as $month) {
$line = explode(',', $data);
echo '<tr><td>', implode('</td><td>', $line), '</td></tr>';
}
echo "</table>";
Edit:
My bad, didn't notice that you were sorting/grouping/totaling. This should set you on the right track, though. The key is to use $line as your source of information. Just compile it into an array and output later (instead of right in the loop).