PHP逐行对变量执行操作

时间:2014-06-21 15:32:49

标签: php line-by-line

我有一个包含html代码的变量,我想对代码的每一行执行操作:

<?php

$html = file_get_contents('http://www.google.com/');

#how i would like it to be (not real commands)
/*
$line = 1
$currentline = readline($html,$line)
line++
where $html is the variable that we want to read a line from
$line is the line number
and $currentline the contents

so if $line is 1 we get that $currentline is <html> or whatever

after that i perform things on that line and continue reading each line untill i read all the lines

*/

?>

我希望你明白我的意思/需要。我是基于网络编程的新手,所以我需要大量的解释!

请不要使用您认为PHP程序员应该知道的技术术语,因为我可能不知道那是什么。

1 个答案:

答案 0 :(得分:1)

您的问题类似于this one,但有人可能会争论答案对于初学者是否可以理解。这应该做你想要的:

<?php

$html = file_get_contents('http://www.google.com/');
// Skipped: Error checks

// Split $html into lines
$lines = explode("\n", $html);

// Iterate over all lines
foreach($lines as $line) {
  // Process $line
}

// Or get the 5th line
$fifth_line = $lines[4];

// Or iterate using an index
for($line = 0; $line < count($lines); $line++) {
  $theline = $lines[$line];
}