Extract data from a page - php

Hello I would like to create a page html and php that is able to take the data in the table contained at this link: http://www.comuni-italiani.it/province.html
I would love to have any tips, I would use the file_get_content but then I do not know how to take all the various data

Can you explain us more clearly what you want to exactly take from this page?
Anyway, to do the trick, you can use file_get_contents to fetch the page then, according to what you want to take from the page (I suppose you want to take every <td> element from the page inside a table), you may use PHP regular expressions (preg_match, preg_match_all) to fetch all the data you need.
Example for your case:
$page = file_get_contents("http://www.comuni-italiani.it/province.html");
$output = array();
preg_match_all('/<td.*.<\/td>/',$page,$output);
print_r($output);
This will output:
Array ( [0] => Array ( [0] => [1] => [2] => Agrigento [3] => Alessandria [4] => Ancona [5] => Aosta [6] => Arezzo [7] => Ascoli Piceno [8] => Asti [9] => Avellino [10] => Bari [11] => Barletta-Andria-Trani [12] => Belluno [13] => Benevento [14] => Bergamo [15] => Biella [16] => Bologna [17] => Bolzano [18] => Brescia [19] => Brindisi [20] => Cagliari [21] => Caltanissetta [22] => Campobasso [23] => Carbonia-Iglesias [24] => Caserta [25] => Catania [26] => Catanzaro [27] => Chieti [28] => Como [29] => Cosenza [30] => Cremona [31] => Crotone [32] => Cuneo [33] => Enna [34] => Fermo [35] => Ferrara [36] => Firenze [37] => Foggia [38] => Forlì-Cesena [39] => Frosinone [40] => Genova [41] => Gorizia [42] => Grosseto [43] => Imperia [44] => Isernia [45] => La Spezia [46] => L'Aquila [47] => Latina [48] => Lecce [49] => Lecco [50] => Livorno [51] => Lodi [52] => Lucca [53] => Macerata [54] => Mantova [55] => Massa-Carrara [56] => Matera [57] => Messina [58] => Milano [59] => Modena [60] => Monza e della Brianza [61] => Napoli [62] => Novara [63] => Nuoro [64] => Olbia-Tempio [65] => Oristano [66] => Padova [67] => Palermo [68] => Parma [69] => Pavia [70] => Perugia [71] => Pesaro e Urbino [72] => Pescara [73] => Piacenza [74] => Pisa [75] => Pistoia [76] => Pordenone [77] => Potenza [78] => Prato [79] => Ragusa [80] => Ravenna [81] => Reggio Calabria [82] => Reggio Emilia [83] => Rieti [84] => Rimini [85] => Roma [86] => Rovigo [87] => Salerno [88] => Medio Campidano [89] => Sassari [90] => Savona [91] => Siena [92] => Siracusa [93] => Sondrio [94] => Taranto [95] => Teramo [96] => Terni [97] => Torino [98] => Ogliastra [99] => Trapani [100] => Trento [101] => Treviso [102] => Trieste [103] => Udine [104] => Varese [105] => Venezia [106] => Verbano-Cusio-Ossola [107] => Vercelli [108] => Verona [109] => Vibo Valentia [110] => Vicenza [111] => Viterbo [112] => CercaNel Sito e sul WebPagine UtiliElenco Province per PopolazionePrincipali Città ItalianeLista Alfabetica RegioniAmministrazioni LocaliScuole in Italia [113] => ) )
which can, of course, be filtered.
In your case, for example, by adding a little foreach loop... :
$page = file_get_contents("http://www.comuni-italiani.it/province.html");
$output = array();
preg_match_all('/<td.*.<\/td>/',$page,$output);
$provinces = array();
foreach ($output as $id => $list) {
for ($i = 2; $i <= 111; $i++) {
array_push($provinces,$list[$i]);
}
}
print_r($provinces);
Will give you this:
Array ( [0] => Agrigento [1] => Alessandria [2] => Ancona [3] => Aosta [4] => Arezzo [5] => Ascoli Piceno [6] => Asti [7] => Avellino [8] => Bari [9] => Barletta-Andria-Trani [10] => Belluno [11] => Benevento [12] => Bergamo [13] => Biella [14] => Bologna [15] => Bolzano [16] => Brescia [17] => Brindisi [18] => Cagliari [19] => Caltanissetta [20] => Campobasso [21] => Carbonia-Iglesias [22] => Caserta [23] => Catania [24] => Catanzaro [25] => Chieti [26] => Como [27] => Cosenza [28] => Cremona [29] => Crotone [30] => Cuneo [31] => Enna [32] => Fermo [33] => Ferrara [34] => Firenze [35] => Foggia [36] => Forlì-Cesena [37] => Frosinone [38] => Genova [39] => Gorizia [40] => Grosseto [41] => Imperia [42] => Isernia [43] => La Spezia [44] => L'Aquila [45] => Latina [46] => Lecce [47] => Lecco [48] => Livorno [49] => Lodi [50] => Lucca [51] => Macerata [52] => Mantova [53] => Massa-Carrara [54] => Matera [55] => Messina [56] => Milano [57] => Modena [58] => Monza e della Brianza [59] => Napoli [60] => Novara [61] => Nuoro [62] => Olbia-Tempio [63] => Oristano [64] => Padova [65] => Palermo [66] => Parma [67] => Pavia [68] => Perugia [69] => Pesaro e Urbino [70] => Pescara [71] => Piacenza [72] => Pisa [73] => Pistoia [74] => Pordenone [75] => Potenza [76] => Prato [77] => Ragusa [78] => Ravenna [79] => Reggio Calabria [80] => Reggio Emilia [81] => Rieti [82] => Rimini [83] => Roma [84] => Rovigo [85] => Salerno [86] => Medio Campidano [87] => Sassari [88] => Savona [89] => Siena [90] => Siracusa [91] => Sondrio [92] => Taranto [93] => Teramo [94] => Terni [95] => Torino [96] => Ogliastra [97] => Trapani [98] => Trento [99] => Treviso [100] => Trieste [101] => Udine [102] => Varese [103] => Venezia [104] => Verbano-Cusio-Ossola [105] => Vercelli [106] => Verona [107] => Vibo Valentia [108] => Vicenza [109] => Viterbo )
(Sorry for the huge arrays).
It is, however, keeping the links inside the array so, if you want to take the values only and NOT the anchor associated to it, just feel free to use another regular expression.
Hope this helps.
(take this as an example, keep in mind that this foreach trick may not work anymore if the page gets changed, I posted it just to give you an idea on how you may have solved that case).

Try to look more into DOMDocument reference : http://php.net/manual/en/class.domdocument.php
Also Those questions might be helpful for you:
Getting DOM elements by classname
PHP Parse HTML code

Related

Last element of array

Im trying to go through array to create a new one from it. Trying to log everything while runnging this code:
$this->writeToLog(print_r($this->assembledText, true), 'ass.log');
foreach ($this->assembledText as $paragraphsKey => $paragraphs) {
$this->writeToLog("Paragraph Key:".$paragraphsKey.print_r($paragraphs, true), 'para.log');
$i = 0;
foreach ($paragraphs as $words) {
$newText[$paragraphsKey][$i] = $words;
$i++;
}
}
Here Im logging the text before i go through, and here what i have so far:
ass.log gives me this:
[2019-05-18 20:32:38] Array
(
[0] => Array
(
[0] => One
[1] => thing
[2] => was
[3] => certain,
[4] => that
[5] => the
[6] => white
[7] => kitten
[8] => had
[9] => had
[10] => nothing
[11] => to
[12] => do
[13] => with
[14] => it:
[15] => —
[16] => it
[17] => was
[18] => the
[19] => black
[20] => kitten’s
[21] => fault
[22] => entirely.
[23] => For
[24] => the
[25] => white
[26] => kitten
[27] => had
[28] => been
[29] => having
[30] => its
[31] => face
[32] => washed
[33] => by
[34] => the
[35] => old
[36] => cat
[37] => for
[38] => the
[39] => last
[40] => quarter
[41] => of
[42] => an
[43] => hour
[44] => (and
[45] => bearing
[46] => it
[47] => pretty
[48] => well,
[49] => considering);
[50] => so
[51] => you
[52] => see
[53] => that
[54] => it
[55] => couldn’t
[56] => have
[57] => had
[58] => any
[59] => hand in
[61] => the
[62] => mischief.
[63] =>
)
[1] => Array
(
[0] =>
[1] => The
[2] => way
[3] => Dinah
[4] => washed
[5] => her
[6] => children’s
[7] => faces
[8] => was
[9] => this:
[10] => first
[11] => she
[12] => held
[13] => the
[14] => poor
[15] => thing
[16] => down
[17] => by
[18] => its
[19] => ear
[20] => with
[21] => one
[22] => paw,
[23] => and
[24] => then
[25] => with
[26] => the
[27] => other
[28] => paw
[29] => she
[30] => rubbed
[31] => its
[32] => face
[33] => all
[34] => over,
[35] => the
[36] => wrong
[37] => way,
[38] => beginning
[39] => at
[40] => the
[41] => nose:
[42] => and
[43] => just
[44] => now,
[45] => as
[46] => I
[47] => said,
[48] => she
[49] => was
[50] => hard
[51] => at
[52] => work on
[54] => the
[55] => white
[56] => kitten,
[57] => which
[58] => was
[59] => lying
[60] => quite
[61] => still
[62] => and
[63] => trying
[64] => to
[65] => purr
[66] => —
[67] => no
[68] => doubt
[69] => feeling
[70] => that
[71] => it
[72] => was
[73] => all
[74] => meant
[75] => for
[76] => its
[77] => good.
[78] =>
)
[2] => Array
(
[0] =>
[1] => But
[2] => the
[3] => black
[4] => kitten
[5] => had
[6] => been
[7] => finished
[8] => with
[9] => earlier
[10] => in
[11] => the
[12] => afternoon,
[13] => and
[14] => so,
[15] => while
[16] => Alice
[17] => was
[18] => sitting
[19] => curled
[20] => up
[21] => in
[22] => a
[23] => corner
[24] => of
[25] => the
[26] => great
[27] => arm-chair,
[28] => half
[29] => talking
[30] => to
[31] => herself
[32] => and
[33] => half
[34] => asleep,
[35] => the
[36] => kitten
[37] => had
[38] => been
[39] => having
[40] => a
[41] => grand
[42] => game
[43] => of
[44] => romps
[45] => with
[46] => the
[47] => ball
[48] => of
[49] => worsted
[50] => Alice
[51] => had
[52] => been
[53] => trying
[54] => to
[55] => wind up,
[57] => and
[58] => had
[59] => been
[60] => rolling
[61] => it
[62] => up
[63] => and
[64] => down
[65] => till
[66] => it
[67] => had
[68] => all
[69] => come
[70] => undone
[71] => again;
[72] => and
[73] => there
[74] => it
[75] => was,
[76] => spread
[77] => over
[78] => the
[79] => hearth-rug,
[80] => all
[81] => knots
[82] => and
[83] => tangles,
[84] => with
[85] => the
[86] => kitten
[87] => running
[88] => after
[89] => its
[90] => own
[91] => tail
[93] => the
[94] => middle.
[95] =>
)
[3] => Array
(
[0] =>
[1] => ‘Oh,
[2] => you
[3] => wicked
[4] => little
[5] => thing!’
[6] => cried
[7] => Alice,
[8] => catching up
[10] => the
[11] => kitten,
[12] => and
[13] => giving
[14] => it
[15] => a
[16] => little
[17] => kiss
[18] => to
[19] => make
[20] => it
[21] => understand
[22] => that
[23] => it
[24] => was
[25] => in
[26] => disgrace.
[27] =>
)
)
And para.log gives me this, like i dont have the last element of array, but i do have an index:
[2019-05-18 20:32:38] Paragraph Key:0Array
(
[0] => One
[1] => thing
[2] => was
[3] => certain,
[4] => that
[5] => the
[6] => white
[7] => kitten
[8] => had
[9] => had
[10] => nothing
[11] => to
[12] => do
[13] => with
[14] => it:
[15] => —
[16] => it
[17] => was
[18] => the
[19] => black
[20] => kitten’s
[21] => fault
[22] => entirely.
[23] => For
[24] => the
[25] => white
[26] => kitten
[27] => had
[28] => been
[29] => having
[30] => its
[31] => face
[32] => washed
[33] => by
[34] => the
[35] => old
[36] => cat
[37] => for
[38] => the
[39] => last
[40] => quarter
[41] => of
[42] => an
[43] => hour
[44] => (and
[45] => bearing
[46] => it
[47] => pretty
[48] => well,
[49] => considering);
[50] => so
[51] => you
[52] => see
[53] => that
[54] => it
[55] => couldn’t
[56] => have
[57] => had
[58] => any
[59] => hand in
[61] => the
[62] => mischief.
[63] =>
)
[2019-05-18 20:32:38] Paragraph Key:1Array
(
[0] =>
[1] => The
[2] => way
[3] => Dinah
[4] => washed
[5] => her
[6] => children’s
[7] => faces
[8] => was
[9] => this:
[10] => first
[11] => she
[12] => held
[13] => the
[14] => poor
[15] => thing
[16] => down
[17] => by
[18] => its
[19] => ear
[20] => with
[21] => one
[22] => paw,
[23] => and
[24] => then
[25] => with
[26] => the
[27] => other
[28] => paw
[29] => she
[30] => rubbed
[31] => its
[32] => face
[33] => all
[34] => over,
[35] => the
[36] => wrong
[37] => way,
[38] => beginning
[39] => at
[40] => the
[41] => nose:
[42] => and
[43] => just
[44] => now,
[45] => as
[46] => I
[47] => said,
[48] => she
[49] => was
[50] => hard
[51] => at
[52] => work on
[54] => the
[55] => white
[56] => kitten,
[57] => which
[58] => was
[59] => lying
[60] => quite
[61] => still
[62] => and
[63] => trying
[64] => to
[65] => purr
[66] => —
[67] => no
[68] => doubt
[69] => feeling
[70] => that
[71] => it
[72] => was
[73] => all
[74] => meant
[75] => for
[76] => its
[77] => good.
[78] =>
)
[2019-05-18 20:32:38] Paragraph Key:2Array
(
[0] =>
[1] => But
[2] => the
[3] => black
[4] => kitten
[5] => had
[6] => been
[7] => finished
[8] => with
[9] => earlier
[10] => in
[11] => the
[12] => afternoon,
[13] => and
[14] => so,
[15] => while
[16] => Alice
[17] => was
[18] => sitting
[19] => curled
[20] => up
[21] => in
[22] => a
[23] => corner
[24] => of
[25] => the
[26] => great
[27] => arm-chair,
[28] => half
[29] => talking
[30] => to
[31] => herself
[32] => and
[33] => half
[34] => asleep,
[35] => the
[36] => kitten
[37] => had
[38] => been
[39] => having
[40] => a
[41] => grand
[42] => game
[43] => of
[44] => romps
[45] => with
[46] => the
[47] => ball
[48] => of
[49] => worsted
[50] => Alice
[51] => had
[52] => been
[53] => trying
[54] => to
[55] => wind up,
[57] => and
[58] => had
[59] => been
[60] => rolling
[61] => it
[62] => up
[63] => and
[64] => down
[65] => till
[66] => it
[67] => had
[68] => all
[69] => come
[70] => undone
[71] => again;
[72] => and
[73] => there
[74] => it
[75] => was,
[76] => spread
[77] => over
[78] => the
[79] => hearth-rug,
[80] => all
[81] => knots
[82] => and
[83] => tangles,
[84] => with
[85] => the
[86] => kitten
[87] => running
[88] => after
[89] => its
[90] => own
[91] => tail
[93] => the
[94] => middle.
[95] =>
)
[2019-05-18 20:32:38] Paragraph Key:3
Any suggestions?
So, i found the solution in the php Bug report: https://bugs.php.net/bug.php?id=60534
Which means that i have to use & (reference) even if i don't need it, just to avoid this bug
PHP version 5.6.33

split big array into multiple arrays where empty element occurs in php

This is Input:-print_r($result4);
Output:-Array ( [0] => A-I-only [1] => B-III-only [2] => C-I-and-II-only [3]=> D-II-and-III-only [4] => E-I,-II,-III [5] => [6] => A-Hepatitis-A [7] => B-Hepatitis-B [8] => C-Hepatitis-C [9] => D-Hepatitis-B-and-C [10] => E-None-of-the-above [11] => [12] => A)-Cholestasis [13] => B)-Cholecystitis [14] => C)-Cholelithiasis [15] => D)-Hepatic-encephalopathy [16] => E)-Ascites [17] => [18] => A-Acetyl-salicylic-acid [19] => B-Ibuprofen [20] => C-Acetaminophen [21] => D-Pepto-Bismol [22] => E-All-of-the-above [23] => [24] => [25] => A-dark-urine [26] => B-stomach-pain [27] => C-blood-in-stools [28] => D-yellowing-of-skin [29] => E-Yellowing-of-eye-and-mucus [30] => [31] => A-Hepatitis-A [32] => B-Hepatitis-B [33] => C-Dukoral [34] => D-Gerdasil [35] => E-None-of-the-above [36] => [37] => cervical-cancer-caused-by-papilloma-virus [38] => A-Glucuronidation [39] => B-Glutathione-conjugation [40] => C-Acetylation [41] => D-Sulfate-conjugation [42] => E-Methylation [43] => [44] => A-Acetylcysteine [45] => B-cysteine [46] => C-Mercapturic-acid [47] => D-Glutathione-conjugation [48] => E-Glutathione-only [49] => [50] => administered-in-teenagers-to-prevent-cervical-cancer. [51] => A-Hepatitis-A [52] => B-Hepatitis-C [53] => C-Hepatitis-D [54] => D-Hepatitis-A,-B-&-C [55] => E-Hepatitis-A,-B-&-D [56] => [57] => [58] => A)-ALT [59] => B)-AST [60] => C)-Bilirubin- [61] => D)-Albumin [62] => E)-Proteins [63] => [64] => A--Saliva- [65] => B-Bile- [66] => C-Pancreatic-duct---- [67] => D-Gastric-secretions [68] => [69] => A)-Sexual-contact [70] => B)-Blood-transfusion [71] => C-Food-and-drink-contamination [72] => D-Traveling-abroad [73] => E-Drugs [74] => [75] => A)-Hepatitis-A-only [76] => B)-Hepatitis-B-only [77] => C)-Hepatitis-A-and-B [78] => D)-Hepatitis-A,-B-and-C [79] => E)-Hepatitis-B-and-C [80] => [81] => [82] => A)-Proton-pump-inhibitors [83] => B)-Warfarin [84] => C)-antacids [85] => D)Lipid-soluble-drugs [86] => E)-Parenteral-drugs [87] => [88] => A)-spironolactone- [89] => B)-NSAIDs- [90] => C)-acetaminophen- [91] => D)-Ibuprofen- [92] => E)-codeine [93] => [94] => A)-constipation- [95] => B)-ascites- [96] => C)-encephalitis- [97] => D)-liver-cirrhosis- [98] => E)-Hepatitis [99] => [100] => encephalitis. [101] => contaminated-hepatitis? [102] => A)-Hepatitis-A [103] => B)-Hepatitis-B [104] => C)-Hepatitis-A-and-B [105] => D)-Hepatitis-C [106] => E)-Hepatitis-D [107] => [108] => A)-Hepatitis-A [109] => [110] => B)-Hepatitis-A-and-B [111] => C)-Hepatitis-B-and-C [112] => D)-Hepatitis-A,-B,-C [113] => [114] => [115] => )
now i want to split array into multiple arrays where empty element occurs like
Array ( [0] => A-I-only [1] => B-III-only [2] => C-I-and-II-only [3]=> D-II-and-III-only [4] => E-I,-II,-III ) Array([6] => A-Hepatitis-A [7] => B-Hepatitis-B [8] => C-Hepatitis-C [9] => D-Hepatitis-B-and-C [10] => E-None-of-the-above )....similarly
Below will work,
$new_array = array();
$i = 0;
foreach($array as $k => $v)
{
if (!empty($v))
{
$new_array[$i][$k] = $v;
continue;
}
$i++;
}
//This will fix your missing fourth index<br/>
$new_array = array_values($new_array);
var_dump($new_array);
DEMO.

Can't run exec()/shell_exec() on 1&1 server

EDIT
I'm trying to run a php file in background without success using one of those function.
This is not the file that will be execute in backgorund (the real one will send emails), I use this method to find out which command parameter is the correct one
File permission 0644; Folder permission: 0755
tets.php:
<html>
<head>
</head>
<body>
<pre>
<?php
$ex='php5 -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php';
exec($ex,$return);
echo print_r($return,true);
?>
</pre>
</body>
</html>
run.php(works from url):
<html>
<head>
</head>
<body>
<pre>
<?php
$fp = fopen('/homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/data.txt', 'w+');
fwrite($fp, '1');
fclose($fp);
echo 'printed';
?>
</pre>
</body>
</html>
these are my tries:
$ex='php5 -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php';
$ex='php -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php';
$ex='/usr/lib/php6 -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php';
Fisrt->
Array
[89] => [79] => [69] => [59] => [49] => [39] => [29] => [19] => [9] => (
[90] => [80] => [70] => [60] => [50] => [40] => [30] => [20] => [10] => [0] => X-Powered-By: PHP/5.2.17
[91] => [81] => [71] => [61] => [51] => [41] => [31] => [21] => [11] => [1] => Content-type: text/html
[92] => [82] => [72] => [62] => [52] => [42] => [32] => [22] => [12] => [2] =>
[93] => [83] => [73] => [63] => [53] => [43] => [33] => [23] => [13] => [3] =>
[94] => [84] => [74] => [64] => [54] => [44] => [34] => [24] => [14] => [4] =>
[95] => [85] => [75] => [65] => [55] => [45] => [35] => [25] => [15] => [5] =>
[96] => [86] => [76] => [66] => [56] => [46] => [36] => [26] => [16] => [6] =>
[97] => [87] => [77] => [67] => [57] => [47] => [37] => [27] => [17] => [7] =>
[98] => [88] => [78] => [68] => [58] => [48] => [38] => [28] => [18] => [8] =>
[99] => [89] => [79] => [69] => [59] => [49] => [39] => [29] => [19] => [9] => Warning: exec() [function.exec]: Unable to fork [php5 -f /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/run.php] in /homepages/23/d293813614/htdocs/amazonmobilewebsites/extendedcomingsoon/admin/test.php on line 8
[100] => [90] => [80] => [70] => [60] => [50] => [40] => [30] => [20] => [10] => Array
[101] => [91] => [81] => [71] => [61] => [51] => [41] => [31] => [21] => [11] => (
[102] => [92] => [82] => [72] => [62] => [52] => [42] => [32] => [22] => [12] => )
[103] => [93] => [83] => [73] => [63] => [53] => [43] => [33] => [23] => [13] =>
[104] => [94] => [84] => [74] => [64] => [54] => [44] => [34] => [24] => [14] =>
[105] => [95] => [85] => [75] => [65] => [55] => [45] => [35] => [25] => [15] =>
[106] => [96] => [86] => [76] => [66] => [56] => [46] => [36] => [26] => )
the second the same array without the warning and the third is an empty array
It's probably disabled on your host, but php itself isn't, so why don't you just include the file?
If your php is going to take a long time to run you can do the following:
ignore_user_abort(true);
set_time_limit(0);
your_long_function();
This will mean your script keeps running indefinitely (you can leave the page), which is potentially a dangerous thing to do. Be careful not to make any infinite loops.
ignore_user_abort
set_time_limit
See this answer for some other options too.

LoL API query for multiple titles from array

I have an array:
Array ( [0] => Ahri [1] => Akali [2] => Alistar [3] => Amumu [4] => Anivia [5] => Annie [6] => Ashe [7] => Blitzcrank [8] => Brand [9] => Caitlyn [10] => Cassiopeia [11] => Cho'Gath [12] => Corki [13] => Dr. Mundo [14] => Evelynn [15] => Ezreal [16] => Fiddlesticks [17] => Fiora [18] => Fizz [19] => Galio [20] => Gangplank [21] => Garen [22] => Gragas [23] => Graves [24] => Hecarim [25] => Heimerdinger [26] => Irelia [27] => Janna [28] => Jarvan IV [29] => Jax [30] => Karma [31] => Karthus [32] => Kassadin [33] => Katarina [34] => Kayle [35] => Kennen [36] => Kog'Maw [37] => LeBlanc [38] => Lee Sin [39] => Leona [40] => Lulu [41] => Lux [42] => Malphite [43] => Malzahar [44] => Maokai [45] => Master Yi [46] => Miss Fortune [47] => Mordekaiser [48] => Morgana [49] => Nasus [50] => Nautilus [51] => Nidalee [52] => Nocturne [53] => Nunu [54] => Olaf [55] => Orianna [56] => Pantheon [57] => Poppy [58] => Rammus [59] => Renekton [60] => Riven [61] => Rumble [62] => Ryze [63] => Sejuani [64] => Shaco [65] => Shen [66] => Shyvana [67] => Singed [68] => Sion [69] => Sivir [70] => Skarner [71] => Sona [72] => Soraka [73] => Swain [74] => Talon [75] => Taric [76] => Teemo [77] => Tristana [78] => Trundle [79] => Tryndamere [80] => Twisted Fate [81] => Twitch [82] => Udyr [83] => Urgot [84] => Varus [85] => Vayne [86] => Veigar [87] => Viktor [88] => Vladimir [89] => Volibear [90] => Warwick [91] => Wukong [92] => Xerath [93] => Xin Zhao [94] => Yorick [95] => Ziggs [96] => Zilean [97] => Lee Sin [98] => Orianna [99] => Yorick [100] => Leona [101] => Riven [102] => Lulu [103] => Hecarim [104] => Varus [105] => Malphite [106] => Morgana [107] => Ryze [108] => Annie [109] => Cho'Gath [110] => Taric [111] => Jax [112] => Teemo [113] => Warwick [114] => Poppy [115] => Garen [116] => Anivia [117] => Blitzcrank [118] => Corki [119] => Karthus [120] => Twitch [121] => Irelia [122] => Miss Fortune [123] => Galio [124] => Swain [125] => Kennen [126] => Malzahar )
I need to use each of these values inside the following function:
$input=file_get_contents("http://leagueoflegends.wikia.com/api.php?action=query&titles=$value&prop=revisions&rvprop=content&format=xml");
Where the $value is a value from the array (eg. Ahri).
How can I achieve this?
Edit: I get these values by doing:
$file = file_get_contents('http://leagueoflegends.wikia.com/api.php?action=query&titles=List_of_champions&prop=revisions&rvprop=content&format=xml');
preg_match_all('/{{ci\|([^}]*)}}/', $file, $matches);
(Where $matches is the array mentioned above.)
Or, you can use the inbuilt way for the LeagueOfLegends API and retrieve all values at the same time by separating each title with the | character. Like so:
$values = array("Ahri","Akali","Alistar");
$input = "";
while($pack = array_splice($values,0,49)){
$concatValues = implode("|",$values); // Will return "Ahri|Akali|Alistar";
$url = "http://leagueoflegends.wikia.com/api.php?action=query&titles=$concatValues&prop=revisions&rvprop=content&format=xml";
$input .= file_get_contents($url);
}
Which for the first three elements of your code, would return the following, you can do it one by one, but the API etiquette suggests that you do the least number of requests to get the data you're after.
<api>
<query>
<pages>
<page pageid="281432" ns="0" title="Ahri">
<revisions>
<rev xml:space="preserve">#REDIRECT [[Ahri the 9 Tails Fox]]</rev>
</revisions>
</page>
<page pageid="5578" ns="0" title="Akali">
<revisions>
<rev xml:space="preserve">#REDIRECT [[Akali the Fist of Shadow]]</rev>
</revisions>
</page>
<page pageid="2202" ns="0" title="Alistar">
<revisions>
<rev xml:space="preserve">#REDIRECT [[Alistar the Minotaur]]</rev>
</revisions>
</page>
</pages>
</query>
</api>
Note that the results are cached, so if you want to get the same data twice, make sure to use the same request and not alter the order of the elements, since a new order would result in a new url and the results being fetched again from their servers instead of serving the API cached version.
Update
Looking at the values in the array, I see some 'titles' that contains quotes and would therefor break the url. You should encode the url with url encode prior to using it.

Only show content of 1 array by it's name

If have and array like this:
Array (
[Example1] => Array
(
[0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] =>
)
[Example2] => Array
(
[0] => [1] => [2] => [3] => [4] =>
[5] => [6] => [7] => [8] => [9] =>
[10] => [11] => [12] => [13] => [14] =>
[15] => [16] => [17] => [18] => [19] =>
[20] => [21] => [22] => [23] => [24] =>
[25] => [26] => [27] => [28] => [29] =>
[30] => [31] => [32] => [33] => [34] =>
[35] => [36] => [37] => [38] => [39] =>
[40] => [41] => [42] => [43] => [44] =>
[45] => [46] => [47] => [48] => [49] =>
[50] => [51] => [52] => [53] => [54] =>
[55] => [56] => [57] => [58] => [59] =>
[60] => [61] => [62] => [63] => [64] =>
[65] => [66] => [67] => [68] => [69] =>
[70] => [71] => [72] => [73] => [74] =>
[75] => [76] => [77] => [78] => [79] =>
[80] => [81] => [82] => [83] => [84] =>
[85] => [86] => [87] => [88] => [89] =>
[90] => [91] => [92] => [93] => [94] =>
[95] => [96] => [97] => [98] => [99] =>
[100] => [101] => [102] => [103] => [104] =>
[105] => [106] => [107] => [108] => [109] =>
[110] => [111] => [112] => [113] => [114] =>
[115] => [116] => [117] => [118] => [119] =>
[120] => [121] => [122] => [123] => [124] =>
[125] => [126] => [127] => [128] => [129] =>
[130] => [131] => [132] => [133] => [134] =>
)
)
There's is 2 primary arrays (Example1 & Example2) and in those another array is made. I would like to know how I can only call only 1 array like "Example2" so it print only that one and ignore "Example1"
Please note that there could be more than 2 primary arrays.
And what if with that array I want to build a drop down menu, here what I have:
$__selectGroups = '';
foreach ($groups as $key => $options)
{
sort($options);
if ($key !== '')
{
$__selectGroups .= '<optgroup label="'.$key.'">';
}
$__selectGroups .= implode("\n", $options);
if ($key !== '')
{
$__selectGroups .= '</optgroup>';
}
}
How can I tell to build the drop down with only Example2 and ignore the others?
I'm not sure I understand the question... seems too simple:
print_r($yourArray['Example1']);
Since you edited your question, I think you want to do this?
foreach ($yourArray['Example1'] as $key => $value) {
}
This paradigm for accessing nested arrays applies across the board...
You need something like this:
echo '<pre>';
print_r($yourArray['Example1']);
print_r($yourArray['Example2']);
echo '<pre>';
Or you need something more elaborated?

Categories