Jump to content

Recommended Posts

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  ;)

Link to comment
https://forums.phpfreaks.com/topic/114997-searching-pages/
Share on other sites

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>';
}

Link to comment
https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-591506
Share on other sites

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

 

??? 

Link to comment
https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-591951
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592688
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592870
Share on other sites

@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....

Link to comment
https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592880
Share on other sites

okay, first thank you both for you help.

 

what does this do?

 

&#160; &#160; &#160; &#160; 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

 

???

Link to comment
https://forums.phpfreaks.com/topic/114997-searching-pages/#findComment-592970
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.