-entropyman Posted July 16, 2008 Share Posted July 16, 2008 Hello, Perhaps someone could help me. I'm trying to search through a html that is essentially just a big txt file. i want to search for the specific occurrence of a word and return the line it on (see example) The page is setup like this: 01 text text text text text text text text 02 text text text text text text text text etc... I've been trying a bunch of different functions, I'm just getting confused :-\ Anyone able to help/point me in the direction to go? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/ Share on other sites More sharing options...
DeanWhitehouse Posted July 16, 2008 Share Posted July 16, 2008 what have u got so far? Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-591394 Share on other sites More sharing options...
-entropyman Posted July 16, 2008 Author Share Posted July 16, 2008 not much I can't figure out which functions to use Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-591402 Share on other sites More sharing options...
Xurion Posted July 16, 2008 Share Posted July 16, 2008 Are you looking for a function that can search for specific strings in the page? Do you know regular expression? Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-591404 Share on other sites More sharing options...
-entropyman Posted July 16, 2008 Author Share Posted July 16, 2008 the closest thing I have gotten so far is for the code to return the first appearance of the searched word via strpos. So all I get is a big number. I want to return the line that has the word on it. Hope this makes a little more sense. ??? Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-591411 Share on other sites More sharing options...
Xurion Posted July 16, 2008 Share Posted July 16, 2008 I just wrote this script that takes $string and splits it into lines. Then searches those lines for the string, in this case, I'm loloking for 'first'. $string = 'This is my first line This is my second line This is my third line'; $line = explode("\n", $string); foreach($line as $value){ $i++; echo 'checking "'.$value.'"<br>'; if(ereg('first', $value)) echo 'Found in line '.$i.': '.ereg_replace('first', '<b>first</b>', $value).'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-591506 Share on other sites More sharing options...
-entropyman Posted July 16, 2008 Author Share Posted July 16, 2008 Thank you Xurion, now I am trying to expand upon that. here's what i have: <?php $name = "{$_POST['name']}"; $server = "{$_POST['server']}"; $string = file_get_contents($server); $line = explode("\n", $string); foreach($line as $value) { $i++; echo 'checking "'.$value.'"<br>'; if(ereg($name, $value)) echo 'Found in line '.$i.': '.ereg_replace('$name', '<b>$name</b>', $value).'<br>'; } ?> now all this produces is a "checking" after every line? Essentially I would like to only return the line with the &name ??? Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-591951 Share on other sites More sharing options...
Xurion Posted July 17, 2008 Share Posted July 17, 2008 Don't put single quotes around your var names, or they will be interpreted as strings. Replace your echo line with the following: echo 'Found in line '.$i.': '.ereg_replace($name, '<b>'.$name.'</b>', $value).'<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592247 Share on other sites More sharing options...
-entropyman Posted July 17, 2008 Author Share Posted July 17, 2008 now i just get blank page <?php $name = "{$_POST['name']}"; $server = "{$_POST['server']}"; $string = file_get_contents($server); $line = explode("\n", $string); foreach($line as $value) { $i++; echo 'checking "'.$value.'"<br>'; if(ereg($name, $value)) echo 'Found in line '.$i.': '.ereg_replace($name, '<b>'.$name.'</b>', $value).'<br>'; } ?> any ideas? ??? hmm and just noticed, shouldn't the second echo be blue? Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592688 Share on other sites More sharing options...
damianjames Posted July 17, 2008 Share Posted July 17, 2008 if(ereg($name, $value)) No curly brace after your if? foreach($line as $value) { $i++; echo 'checking "'.$value.'"<br>'; if(ereg($name, $value)) { echo 'Found in line '.$i.': '.ereg_replace($name, '<b>'.$name.'</b>', $value).'<br>'; } } Rookie talking here, but that's what I see. Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592870 Share on other sites More sharing options...
unkwntech Posted July 17, 2008 Share Posted July 17, 2008 @damianjames - if you only using one command after an if you don't NEED the {}. Now back to the question at hand. <?php $name = "{$_POST['name']}"; //<----- heres your problem $server = "{$_POST['server']}"; // I tested this without the "{ and the }" and it works jsut fine. $string = file_get_contents($server); $line = explode("\n", $string); foreach($line as $value) { $i++; echo 'checking "'.$value.'"<br>'; if(ereg($name, $value)) echo 'Found in line '.$i.': '.ereg_replace($name, '<b>'.$name.'</b>', $value).'<br>'; } ?> Should be: <?php $name = $_POST['name']; $server = $_POST['server']; $string = file_get_contents($server); $line = explode("\n", $string); foreach($line as $value) { $i++; echo 'checking "'.$value.'"<br>'; if(ereg($name, $value)) echo 'Found in line '.$i.': '.ereg_replace($name, '<b>'.$name.'</b>', $value).'<br>'; } ?> For and example http://www.unkwndesing.com/test.php?name=<html I have hard codded the $server so there will be no browsing of my files.... Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592880 Share on other sites More sharing options...
unkwntech Posted July 17, 2008 Share Posted July 17, 2008 oops that should have been http://www.unkwndesign.com/test.php?name=<html Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592925 Share on other sites More sharing options...
-entropyman Posted July 18, 2008 Author Share Posted July 18, 2008 okay, first thank you both for you help. what does this do?         echo 'Found in line '.$i.': '.ereg_replace($name, '<b>'.$name.'</b>', $value).'<br>'; now once again i just get "checking " after every line, but thats after i take out the 160's ??? Quote Link to comment https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592970 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.