Php preg_match,正在尝试查找数组项并清理结果。。。正则表达式发行
我正在尝试将一些数据从外部站点解析为本地脚本。本地脚本使用一些get数据调用远程页面以启动响应。 在远程脚本的头中返回所需的数据,在本例中,数据是“key”字符串 现在,远程页面还输出一堆其他标题,我们所关注的标题称为LicenceID unfourtunalty由get_headers创建的数组似乎在值中包含头标题,而不是键。因此,我尝试使用一些正则表达式来匹配标头值,看起来像这样:Php preg_match,正在尝试查找数组项并清理结果。。。正则表达式发行,php,regex,preg-match,Php,Regex,Preg Match,我正在尝试将一些数据从外部站点解析为本地脚本。本地脚本使用一些get数据调用远程页面以启动响应。 在远程脚本的头中返回所需的数据,在本例中,数据是“key”字符串 现在,远程页面还输出一堆其他标题,我们所关注的标题称为LicenceID unfourtunalty由get_headers创建的数组似乎在值中包含头标题,而不是键。因此,我尝试使用一些正则表达式来匹配标头值,看起来像这样: function check_path($path) { $headers = @get_heade
function check_path($path)
{
$headers = @get_headers($path);
$licenseID = preg_grep('~licenceID~i', $headers);
$licenseID = preg_replace('~licenceID:\s+~i', '', $licenseID); // clean-up
if($licenseID)
{
return $licenseID['7'];
}
else
{
return false;
}
}
许可证编号:DFKFDLSDLLGHGSLDGHSLD
function check_path($path)
{
$headers = @get_headers($path);
var_dump($headers);
//while (list($key, $value) = each($headers))
foreach($headers as $key => $value)
{
echo "$key - $value<br />";
//if(preg_match('/^.*licenceID:*$/',$value))
if (preg_match('/^.*licenceID.*$/i', $dictionary, $matches))
{
echo "found";
echo "$key - $value<br />";
$position = strpos($value, ':');
$clean_value = substr($value, $position);
$clean_up = trim_string($clean_value,":");
return $clean_up;
}
else
{
return false;
}
}
}
功能检查路径($path)
{
$headers=@get_headers($path);
变量转储($headers);
//while(列表($key,$value)=每个($headers))
foreach($key=>$value的标题)
{
回显“$key-$value
”;
//if(预匹配('/^.*许可证ID:$/',$value))
if(preg_match('/^.*许可证ID.$/i',$dictionary,$matches))
{
回声“发现”;
回显“$key-$value
”;
$position=strpos($value',:');
$clean_value=substr($value,$position);
$clean\u up=trim\u字符串($clean\u值,“:”);
退还$clean_;
}
其他的
{
返回false;
}
}
}
如果(preg_match('/^.licenseid.$/i',$dictionary,$matches))行中的正则表达式有问题
似乎无法使其匹配,还需要从值中删除LicenseID:,并仅返回以下值之后的数据:
欢迎您的建议 您可以使用():
功能检查路径($path)
{
$headers=@get_headers($path);
变量转储($headers);
foreach($key=>$value的标题)
{
回显“$key-$value
”;
if('licenseId:'==substr($value,0,10))
{
回声“发现”;
回显“$key-$value
”;
$clean_value=substr($value,10);
$clean\u up=修剪($clean\u值);
退还$clean_;
}
其他的
{
返回false;
}
}
}
我认为您遇到了问题,因为您在foreach循环中返回false,除非licenseid
是第一个头,它将始终返回false。以下是一些简化的解决方案:
使用
或者,像建议的那样:
$headers = @get_headers($path, 1);
$licenseID = $headers['licenceID'];
我还没有对此进行测试,但它应该可以工作。所以最终的函数如下所示:
function check_path($path)
{
$headers = @get_headers($path);
$licenseID = preg_grep('~licenceID~i', $headers);
$licenseID = preg_replace('~licenceID:\s+~i', '', $licenseID); // clean-up
if($licenseID)
{
return $licenseID['7'];
}
else
{
return false;
}
}
感谢Alix Axel的preg_grep建议,并在return false else声明中发现了错误。当然,它总是返回错误!doh.要删除数据使用捕获组,则id将位于$matches[]数组中的某个位置,具体取决于您如何创建捕获组。至于你的代码不匹配,它看起来很好,它应该匹配任何有单词licenseID在它,你能显示一个确切的输入?顺便说一句,字典似乎并没有在该函数的作用域中定义,现在我看到了它。。。这也许可以解释。我刚刚记得get_headers有第二个参数,如果为true,则键将是header名称。就这样!现在我可以瞄准钥匙了!
function check_path($path)
{
$headers = @get_headers($path);
$licenseID = preg_grep('~licenceID~i', $headers);
$licenseID = preg_replace('~licenceID:\s+~i', '', $licenseID); // clean-up
if($licenseID)
{
return $licenseID['7'];
}
else
{
return false;
}
}