-1

我有一个包含 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 程序员应该知道的技术术语,因为我可能不知道那是什么。

4

1 回答 1

1

您的问题与问题相似,尽管有人可能会争论答案对于初学者是否可以理解。这应该可以满足您的需求:

<?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];
}
于 2014-06-21T16:11:05.437 回答