如何使用PHP取消for循环中的元素范围? [英] How to uncapture the range of elements in for loop using PHP?
问题描述
示例:
@article {boonzaier2009development,
author =Boonzaier,A。和Schubach,K。和Troup,K和Pollard,A。和Aranda,S。和Schofield,P。,
title =心理教育干预的发展,
journal =心理社会杂志肿瘤科,
体积=27,
数=1,
pages =136-153,>
year = 2009
}
@book {bottoff2008women,
author =Bottoff ,JL和Oliffe,JL和Halpin,M。和Phillips,M。和McLean,G。和Mroz,L。,
title =妇女和前列腺癌支持团体:{The}性别connect?{Social} {Science}& {Medicine},
publisher =66,
pages =1217-1227,
年= 2008
}
@article {bottorff2012gender,
author =Bottorff,JL和Oliffe,JL和Kelly,M。,
title =r中的性别oom,
journal =定性健康研究,
volume =22,
number =4,>
pages =435-440,
year = 2012
}
我想捕获双引号之间的字符串仅@article部分。我得到@article的数量和@article字段的范围来获取@article元素的值。使用for循环获取@article的值(对于循环值:@article到next @article的范围等等)问题是,例如第一个字符串@article在第10行,第二个在第18行,am在这个范围和获取值之间进行循环,但是,介于@book之间也是如此,以便如何消除for循环中的@book范围。因为它捕获了@book元素,因为它位于@article的范围内。
php代码:
<?php
$ file = file( master.bib跨度>);
$ typeart = array();
$ cont = array();
// 文章数量 span>
$ key = ' @ article';
foreach ($ file as $ l => $ line){
if (strpos($ line,$ key)!== false){
$ l ++ ;
$ typeart [] = $ l ;
}
} // 文章的最终数量
$ counttypeart = count($ typeart);
for($ j = 0; $ j< $ counttypeart; $ j ++){
for($ i = $ typeart [$ j]; $ i< $ typeart [$ j + 1]; $ i ++){
if(strpos($ file [$ i],' 作者')){
preg_match(' / \(。*?) \/跨度>,$文件[$ i]于,$续);
$ author = $ cont [1];
echo $ author ;
echo ;
}
if(strpos($ file [$ i],' title')){
preg_match(' / \(。*?)\/'< /跨度>,$文件[$ i]于,$续);
$ title = $ cont [1];
echo $ title ;
echo ;
}
if(strpos($ file [$ i],' journal')){
preg_match(' / \(。*?)\/'< /跨度>,$文件[$ i]于,$续);
$ journal = $ cont [1];
echo $ journal ;
echo ;
}
if(strpos($ file [$ i],' volume ')){
preg_match(' / \(。*?)\ /跨度>,$文件[$ i]于,$续);
$ volume = $ cont [1];
echo $ volume ;
echo ;
}
if(strpos($ file [$ i],' number ')){
preg_match(' / \(。*?)\ /跨度>,$文件[$ i]于,$续);
$ number = $ cont [1];
echo $ number ;
echo ;
}
if(strpos($ file [$ i],' pages ')){
preg_match(' / \(。*?)\ /跨度>,$文件[$ i]于,$续);
$ pages = $ cont [1];
echo $ pages ;
echo ;
echo ;
}
}
}
?>
预期输出(来自上述例子):
Boonzaier,A。和Schubach,K。和Troup, K.和Pollard,A。和Aranda,S。和Schofield P.
为患有前列腺癌的男性开展心理教育干预
心理社会肿瘤学杂志
27
1
136-153
Bottorff ,JL和Oliffe,JL和Kelly,M。
房间性别
定性健康研究
22 >
4
435-440
file = file( master.bib);
typeart = array();
CONT =阵列();
// 文章数量 span>
Example:
@article{boonzaier2009development,
author = "Boonzaier, A. and Schubach, K. and Troup, K. and Pollard, A. and Aranda, S. and Schofield, P.",
title = "Development of a psychoeducational intervention ",
journal = "Journal of Psychosocial Oncology",
volume = "27",
number = "1",
pages = "136-153",
year = 2009
}
@book{bottoff2008women,
author = "Bottoff, J. L. and Oliffe, J. L. and Halpin, M. and Phillips, M. and McLean, G. and Mroz, L.",
title = "Women and prostate cancer support groups: {The} gender connect? {Social} {Science} & {Medicine}",
publisher = "66",
pages = "1217-1227",
year = 2008
}
@article{bottorff2012gender,
author = "Bottorff, J. L. and Oliffe, J. L. and Kelly, M.",
title = "The gender (s) in the room",
journal = "Qualitative Health Research",
volume = "22",
number = "4",
pages = "435-440",
year = 2012
}
I want to capture the string between double quotes of @article part only. Am getting the count of @article and range of @article fields to get the values of @article elements. Using for loop am getting values of @article (for loop values: range of @article to next @article and so on) The problem is, for example first string @article is in 10th line and second one is in 18 th line, am doing for loop between this range and getting the value but, inbetween @book also is there so how to eliminate that @book range of lines in for loop. Because it captures @book elements also as it is inside in the range of @article.
php code:
<?php
$file=file("master.bib");
$typeart=array();
$cont=array();
//count of article
$key = '@article';
foreach ($file as $l => $line) {
if (strpos($line,$key) !== false) {
$l++;
$typeart[]= $l;
}
}//end-count of article
$counttypeart=count($typeart);
for($j=0;$j<$counttypeart;$j++){
for($i=$typeart[$j];$i<$typeart[$j+1];$i++){
if(strpos($file[$i],'author')){
preg_match('/\"(.*?)\"/',$file[$i],$cont);
$author= $cont[1];
echo $author;
echo ;
}
if(strpos($file[$i],'title')){
preg_match('/\"(.*?)\"/',$file[$i],$cont);
$title= $cont[1];
echo $title;
echo ;
}
if(strpos($file[$i],'journal')){
preg_match('/\"(.*?)\"/',$file[$i],$cont);
$journal= $cont[1];
echo $journal;
echo ;
}
if(strpos($file[$i],'volume')){
preg_match('/\"(.*?)\"/',$file[$i],$cont);
$volume= $cont[1];
echo $volume;
echo ;
}
if(strpos($file[$i],'number')){
preg_match('/\"(.*?)\"/',$file[$i],$cont);
$number= $cont[1];
echo $number;
echo ;
}
if(strpos($file[$i],'pages')){
preg_match('/\"(.*?)\"/',$file[$i],$cont);
$pages= $cont[1];
echo $pages;
echo ;
echo ;
}
}
}
?>
Expected output (From above mentioned example):
Boonzaier, A. and Schubach, K. and Troup, K. and Pollard, A. and Aranda, S. and Schofield P.
Development of a psychoeducational intervention for men with prostate cancer
Journal of Psychosocial Oncology
27
1
136-153
Bottorff, J. L. and Oliffe, J. L. and Kelly, M.
The gender (s) in the room
Qualitative Health Research
22
4
435-440
file=file("master.bib");
typeart=array();
cont=array(); //count of article
这篇关于如何使用PHP取消for循环中的元素范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!