Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中,如何有效地从整个cURL响应中提取一个特定的行?_Php_Performance - Fatal编程技术网

在php中,如何有效地从整个cURL响应中提取一个特定的行?

在php中,如何有效地从整个cURL响应中提取一个特定的行?,php,performance,Php,Performance,通过使用cURL,我得到了$str的响应$str是html格式的,总是由327行组成。我想从第127行提取日期,它原来是 <b>Last Updated On:</b>17-Dec-2011 11:33:41 UTC<br><br> 上次更新日期:2011年12月17日11:33:41 UTC 为了尽量减少$str使用的内存,在cURl之后,我立即这样做 $str=带标签($str) 然后,要获取完整的行 preg_match(“/^上次更新日

通过使用cURL,我得到了$str的响应$str是html格式的,总是由327行组成。我想从第127行提取日期,它原来是

<b>Last Updated On:</b>17-Dec-2011 11:33:41 UTC<br><br>
上次更新日期:2011年12月17日11:33:41 UTC

为了尽量减少$str使用的内存,在cURl之后,我立即这样做

$str=带标签($str)

然后,要获取完整的行

preg_match(“/^上次更新日期为………/m”,$str,$line)

$lastupdate=$line[0]

然后,要删除多余的文本

$lastupdate=str_replace(“上次更新日期:”,“,$lastupdate”)

现在我认为这个preg_match语句不是最有效的。它使用了太多的点,一定有类似的东西

LookFor=上次更新日期:

如果看到LookFor,则获取接下来的25个字符

我是否使用正确的preg_match syntex?目前它正在发挥作用,并给了我想要的结果,但我不知道它是如何工作的

请向我推荐正确/有效的preg赛道

另外,我如何告诉PHP直接跳到第127行,而不是遍历所有前126行。所有具有[CR][LF]结尾的行

附言

  • 接下来我只需要25个字符
  • 我需要的线路永远是127号
  • 我使用检索到的日期作为文本字符串;无计算,仅存储在.csv文件中。但如果我能用合适的日期格式来代替文本,我会非常高兴的。现在它只是一个文本字符串

试试这个:

<?php

    // get the lines in an array
    $lines = explode("\n",$str);

    // get the 127th line
    $line = $lines[126];

    // get the date
    preg_match('/<\/b>(.+)<br><br>/', $line, $matches);

    $date = $matches[1];

    echo $date;

?>
17-Dec-2011 11:33:41 UTC

试试这个:

<?php

    // get the lines in an array
    $lines = explode("\n",$str);

    // get the 127th line
    $line = $lines[126];

    // get the date
    preg_match('/<\/b>(.+)<br><br>/', $line, $matches);

    $date = $matches[1];

    echo $date;

?>
17-Dec-2011 11:33:41 UTC

谢谢,这正是我所需要的,而且代码更好看。@Davinder真的很欢迎你,我的朋友!总是乐于助人!:-)如果您发现某个答案特别有用,建议您将其标记为答案(答案旁边的勾号),以便社区能够从中受益。谢谢,这正是我所需要的,而且代码更好看。@Davider非常欢迎您,我的朋友!总是乐于助人!:-)如果您发现某个答案特别有用,建议将其标记为答案(答案旁边的勾号),以便社区能够从中受益。