Using find('list') in Cakephp 1.3 with custom fields - php

I'm working in a cakephp 1.3 project. I have a problem retrieving some data from my database using find('list'). Before my last change I had this code:
$listaHabilidades = $this->Habilidad->find('list',array('fields'=>array('id','habilidad'),'order'=>array('habilidad'))));
This command gives me the next array:
Array
(
[40] => Bicicleta
[42] => enganches de poleas
[28] => Escalada
[43] => transfer
[41] => 4x4
[53] => Administración Linux
[72] => Ángeles
[59] => Baile Flamenco
[57] => Baloncesto
[39] => Barranquismo
[66] => Cante Flamenco
[30] => Conducción
[52] => Consola Linux
[80] => cuarta prueba
[75] => Demonios
[84] => Esgrima
[58] => Futbol
[76] => Limpiar
[77] => Limpiar2
[54] => Linux
[27] => Montañismo
[60] => MS Office
[65] => Natación
[45] => Patinaje
[56] => PC
[78] => probar
[44] => Programación
[82] => Protocolo
[81] => quinta prueba
[63] => Tenis
[79] => tercera prueba
[83] => Triatlón
[55] => W8
[51] => XP
[64] => Zapateado
)
But I had a problem because the data can have spaces at the begining of the field 'habilidad' and I want to get the data without the and order them also without them. So I changed the command to:
$listaHabilidades = $this->Habilidad->find('list',array('fields'=>array('id','trim(Habilidad.habilidad)'),'order'=>array('trim(Habilidad.habilidad)'))));
It gives me the next array:
Array
(
[41] =>
[53] =>
[72] =>
[59] =>
[57] =>
[39] =>
[40] =>
[66] =>
[30] =>
[52] =>
[80] =>
[75] =>
[42] =>
[28] =>
[84] =>
[58] =>
[76] =>
[77] =>
[54] =>
[27] =>
[60] =>
[65] =>
[45] =>
[56] =>
[78] =>
[44] =>
[82] =>
[81] =>
[63] =>
[79] =>
[43] =>
[83] =>
[55] =>
[51] =>
[64] =>
)
How can I get the list trimmed (without loops)?

Can you add a virtual field to your model:
public $virtualFields = array(
't_habilidad' => 'LTRIM(Habilidad.habilidad)'
);
Then use
$listaHabilidades = $this->Habilidad->find('list', array('fields'=>array('id','habilidad'), 'order' => array('t_habilidad'))));

Not exactly sure how CakePHP generates the query internally, but you can just trim it in php after retrieval
$listaHabilidades = $this->Habilidad->find('list',array('fields'=>array('id','habilidad'),'order'=>array('habilidad'))));
$listaHabilidades = array_map('trim', $listaHabilidades);
// voila

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

Remove consecutive occurrences of string "start" from array PHP ARRAY

I have the following array
Array ( [0] => **start** [1] => **start** [2] => name [3] => producer [4] => contact name [5] => 03354222271 [6] => fzahid001#gmail.com [7] => day contact name [8] => 03354222271 [9] => venue name [10] => adress [11] => country [12] => city [13] => desc [14] => file [15] => 2016-01-01 [16] => 01:00 [17] => 06:00 [18] => 2 [19] => music_festival [20] => 2000+ [21] => quick [22] => alcohol [23] => quick [24] => 10x10 [25] => ***no*** [26] => ***no*** [27] => ***no*** [28] => 2 [29] => 0 [30] => 4 [31] => $158 [32] => $118.5 [33] => $284 [34] => $960 [35] => **start** [36] => na [37] => producer [38] => con [39] => 1 [40] => fzahid001#gmail.com [41] => nam [42] => 1 [43] => venue [44] => ad [45] => co [46] => ci [47] => description test [48] => download555ssss.png [49] => 2016-12-07 [50] => 13:00 [51] => 19:00 [52] => 2 [53] => manual_selection [54] => ATV [55] => 10x10 [56] => no [57] => no [58] => no [59] => 1 [60] => 1 [61] => 1 [62] => $109.5 [63] => $118.5 [64] => $99.5 [65] => $728 [66] => **start** [67] => Race [68] => Race Club [69] => Faizan [70] => 03354222271 [71] => fzahid001#gmail.com [72] => Faizan Zahid [73] => 03354222271 [74] => DHA [75] => 90-H Tariq Gardens [76] => Pakistan [77] => Lahore [78] => [79] => images.jpg [80] => 2017-01-01 [81] => 00:00 [82] => 07:00 [83] => 2 [84] => cycling_road_race [85] => 1_field [86] => 1_399 [87] => electronic [88] => quick [89] => 10x10 [90] => no [91] => no [92] => no [93] => 1 [94] => 0 [95] => 1 [96] => $109.5 [97] => $118.5 [98] => $99.5 [99] => $796.5 [100] => )
Now what I want to do is, remove the consecutive duplicates of "start". I am currently using following code
foreach ($updateddata as $value){
if($value != $previousvalue){
array_push($finaldata, $value);
}
$previousvalue = $value;
}
but is also removes other consecutive duplicates as well which I don't want to remove. Kindly help me how to do this.
I have highlighted the occurrences of "start" which I want to remove and make it one single entry, while leaving the other string as it is e.g. I don't want to remove the duplicates of string "no"
Try this
if($value != $previousvalue){
array_push($finaldata, $value);
} else if( $value != 'start'){
array_push($finaldata, $value);
}
To remove the consecutive duplicates of "start" value you should restrict the crucial condition to that value. Use the following approach:
// exemplary array
$updateddata = [
'start', 'start', 'producer', 'contact', 'no', 'no'
];
foreach ($updateddata as $k => $v) {
if (isset($updateddata[$k-1]) && $v == 'start' && $v == $updateddata[$k-1]) {
unset($updateddata[$k]);
}
}
The output:
Array
(
[0] => start
[2] => producer
[3] => contact
[4] => no
[5] => no
)

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.

Extract data from a page

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

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.

Categories