-1

I have a variable containing an html code and I want to perform an action of every line of the code:

<?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

*/

?>

I hope you understand what I mean/need. I’m very new to web based programming so I need a lot of explanation!

Please don’t use technical terms you think a PHP programmer should know because i do probably don't know what that is.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
downrep_nation
  • 432
  • 1
  • 7
  • 16
  • `file_get_html` should be used for DOM operations. What are you trying to do? Why do you process it line by line? – Tobias Jun 21 '14 at 15:40
  • i was searching for a libary to do something,well i fixed it now that i knw i can use a diffrent command for that. – downrep_nation Jun 21 '14 at 15:49

1 Answers1

1

Your question is similar to this one although one might argue about whether the answers are understandable for a beginner. This should do what you're looking for:

<?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];
}
Community
  • 1
  • 1
Tobias
  • 7,723
  • 1
  • 27
  • 44
  • very interesting! i will take a look at it! im happy that you posted that :) but why explode ._. and what is foreach($lines as $line) all about? – downrep_nation Jun 21 '14 at 16:14
  • the line with explode gives me an error Parse error: syntax error, unexpected T_VARIABLE – downrep_nation Jun 21 '14 at 16:20
  • the function explode does exsist..but gives an error :) someone must have had fun while naming these commands @still_learning – downrep_nation Jun 21 '14 at 16:20
  • This example works for me, there should not be any syntax errors. You can find answers to questions about `explode` and `foreach` at [php.net](http://php.net/manual/control-structures.foreach.php), or just use [Google](https://google.com). – Tobias Jun 21 '14 at 16:31
  • ahm @still_learning i do not know why foreach is not working. – downrep_nation Jun 21 '14 at 16:33
  • wow @still_learning everything works now! i would like to thank you alot! please make this the final answer ^ – downrep_nation Jun 21 '14 at 16:43
  • That's your task, just check the green tick on the left. – Tobias Jun 21 '14 at 16:43
  • did it @still_learning :) as you can see im new to SO - but one day i will be good like you guys,like in every other language i tried to learn – downrep_nation Jun 21 '14 at 16:44