问题
If I have an array:
Array
(
[0] => Array
(
[0] => |174|September|2001|
[1] => |Pengantar=Hello!!!!
[2] => |Tema= Sami Mawon
[3] => |Tema_isi=meet you!!!
[4] => |Kutip=people
[5] => |Kutip_kitab=Efesus
[6] => |Kutip_pasal=4
[7] => |Kutip_ayat=28
[8] => |Tema_sumber=Kiriman dari Maurits albert (romind@ )
[9] => [[Kategori:e-humor 2001]]
)
How can I get the value of Pengantar, Tema, Tema_isi etc?
回答1:
You're going to have to loop over the array and use preg_match with a reference. Regex something like this (off the top of my head) would probably work:
/\|(.*?)=(.*?)|?/
Just use preg_match('/\|(.*?)=(.*?)|?/', $subject[$x], $matches);
and var_dump($matches);
to see the results.
Don't forget that the $matches array passed into the preg_match function is a reference to an array which you should instantiate first and that it will be overwritten in each loop cycle.
回答2:
just use array walk, a matching reg ex and a lambda function:
$array = array(
array(
'|174|September|2001|',
'Pengantar=Hello!!!!',
'Tema= Sami Mawon ',
'Kategori:e-humor 2001',
[...]
)
);
$values = array();
array_walk($array[0],function(&$item1, $key) use(&$values) {
if(preg_match('#[^=]=(.+)#',$item1,$match)){
$values[] = $match[1];
}
});
print_r($values);
回答3:
The regular expression can be like this, using the named subpattern of "preg_match()":-
$a = array(
array(
'|174|September|2001|',
'|Pengantar=Hello!!!!',
'|Tema= Sami Mawon',
'|Tema_isi=meet you!!!',
'|Kutip=people',
'|Kutip_kitab=Efesus',
'|Kutip_pasal=4',
'|Kutip_ayat=28',
'|Tema_sumber=Kiriman dari Maurits albert (romind@ )',
'[[Kategori:e-humor 2001]]',
)
);
$pattern = '/(?<first>\w+)[:=](?<rest>[\d|\w|\s]+)/';
$matches = array();
foreach ($a as $_arrEach) {
foreach ($_arrEach as $_each) {
$result = preg_match($pattern, $_each, $matches[]);
}
}
echo "<pre>";
print_r($matches);
echo "</pre>";
You will find that the array key "first
" satisfies your requirement.
The above will output as:-
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => Pengantar=Hello
[first] => Pengantar
[1] => Pengantar
[rest] => Hello
[2] => Hello
)
[2] => Array
(
[0] => Tema= Sami Mawon
[first] => Tema
[1] => Tema
[rest] => Sami Mawon
[2] => Sami Mawon
)
[3] => Array
(
[0] => Tema_isi=meet you
[first] => Tema_isi
[1] => Tema_isi
[rest] => meet you
[2] => meet you
)
[4] => Array
(
[0] => Kutip=people
[first] => Kutip
[1] => Kutip
[rest] => people
[2] => people
)
[5] => Array
(
[0] => Kutip_kitab=Efesus
[first] => Kutip_kitab
[1] => Kutip_kitab
[rest] => Efesus
[2] => Efesus
)
[6] => Array
(
[0] => Kutip_pasal=4
[first] => Kutip_pasal
[1] => Kutip_pasal
[rest] => 4
[2] => 4
)
[7] => Array
(
[0] => Kutip_ayat=28
[first] => Kutip_ayat
[1] => Kutip_ayat
[rest] => 28
[2] => 28
)
[8] => Array
(
[0] => Tema_sumber=Kiriman dari Maurits albert
[first] => Tema_sumber
[1] => Tema_sumber
[rest] => Kiriman dari Maurits albert
[2] => Kiriman dari Maurits albert
)
[9] => Array
(
[0] => Kategori:e
[first] => Kategori
[1] => Kategori
[rest] => e
[2] => e
)
)
Hope it helps.
回答4:
Apart from the question of "which pattern", I suggest you take a look into preg_replaceDocs which is able to operate on arrays directly.
$pattern = "...";
$matches = preg_replace($pattern, '\1', $array);
回答5:
It is not clear from the question, but it looks like you want preg_filter.
This function can perform a regex match and replace on an array, returning a new array containing only the replaced values of the matched items.
来源:https://stackoverflow.com/questions/8831775/how-to-use-preg-match-in-array