我有这个数组$nodup_filter
:
Array(
[0] => <tr><td>29/06/2015</td><td>19:35</td><td>12345 Column information</td><td>67899 Column information - 12</td><td>Rawling Spencer, Peter; Smith Johnson, Katherine</td><td>More information</td></tr>
[1] => <tr><td>12/09/2015</td><td>10:12</td><td>98545 Column information</td><td>67659 Column information - 32</td><td>Cattle Gamme, Mark</td><td>More information</td></tr>
[2] => <tr><td>11/02/2015</td><td>12:40</td><td>59675 Column information</td><td>94859 Column information - 11</td><td>Davis Miller, Patrick; Brown Moore,Stephan; Taylor Jones, Mary</td><td>More information</td></tr>
[3] => <tr><td>01/10/2015</td><td>20:12</td><td>69365 Column information</td><td>78464 Column information - 63</td><td>Willson Chowell, Adrianne; Jackson Bolwin, Stella</td><td>More information</td></tr>
)
如果月份是> 07,然后我希望在脚本名后面返回“1”,如果date是&lt; = 07那么将返回“2”。 完成!!
从第二列我得到第一个代码和破折号“ - ”之后的代码。如果此代码介于50和9之间,则需要对其进行转换,将第一个数字放在单位位置。然后十位可能为零。当数字为50或以上时,数字保持不变。如果它是9或更低,则必须在十位添加零。 完成!
每个用户都以分号分隔“;”在同一列中。到现在为止,我可以获得每个用户的第一个姓氏,但我希望此列中的每个用户都能获得第一个姓氏。如果有多个用户,那么我希望每个姓氏都能复制脚本名称。
希望通过以下示例更清楚。
这是预期的输出:
scriptname 2 67899-12 Rawling
scriptname 2 67899-12 Smith
scriptname 1 67659-32 Cattle
scriptname 2 94859-11 Davis
scriptname 2 94859-11 Brown
scriptname 2 94859-11 Taylor
scriptname 1 78464-63 Willson
scriptname 1 78464-63 Jackson
这个代码用正则表达式来过滤它并得到第二列信息代码和数组中每个元素的第一个姓氏:
foreach($nodup_filter as $filtered) {
/* regex */
$nodup_filtertoexec = preg_match('/\d{2}\/(\d{2})\/\d{4}.*?\d+\s.*?(\d+)\s.*?-\s(\d+).*?(?:.*?<td>){1}([a-zA-ZñÑ]+).*/m', $filtered, $matches);
/* regex end */
/* After dash "-" code management */
if (isset($matches[3])){
if (($matches[3]<50) && ($matches[3]>9)) {
$matches[3] = floor($matches[3]/10);
} else if ($matches[3]<10){
$matches[3] = str_pad($matches[3], "0", STR_PAD_LEFT);
}
/* End after dash "-" code management */
/* Taking 1 or 2 by month number */
if($matches[1]<=7) {
$matches[1]="2";
} else{
$matches[1]="1";
}
/* End Taking 1 or 2 by month number code */
/* Building final variable */
$format = '%d %d-%02d %s';
$result = sprintf($format, $matches[1], $matches[2], $matches[3], $matches[4]);
echo 'scriptname '.$result.'<br>';
/* End building final variable */
}else{
$badresults[] = $filtered;
}
echo "<br><h2>Rows with some empty information : </h2><br>";
echo $tableinit;
foreach($badresults as $bad_results) echo $bad_results;
echo $tableend;
以下是实际输出:
scriptname 67899-12 Rawling
scriptname 67659-32 Cattle
scriptname 94859-11 Davis
scriptname 78464-63 Willson
再次感谢!!
答案 0 :(得分:1)
不需要复杂的正则表达式:
foreach($arr as $a){
$a=str_replace(array('<tr>','</td>'),'',$a); //remove every <tr> and </td>
$x=explode('<td>',$a); //split in columns
//MONTH
//get month part of date, make int and compare
$month=1;
if((int)substr($x[1],3,2)<7)$month=2;
//CODE
//just delete some parts of the string in column
$code=str_replace('Columninformation','',str_replace(' ','',$x[4]));
//SURNAME
$n=explode(';',$x[5]); //split in names
$names=array();
foreach($n as $v){
//the replace is needed if only 1 surname exists
$names[]=str_replace(',','',strtok($v," "));
}
//loop all found names and create result array
foreach($names as $n){
$res[]='scriptname '.$month.' '.$code.' '.$n.'<br>';;
}
}