Jump to content

Recommended Posts

I would like to creat a script that searched files on my server for a string once the string was found it would desply the area were it found it, how would i do this??
[code]
<?php
$search = $_POST['s'];
$file="index.php";
$getcontents=file_get_contents($file);
  

if (preg_match("/$search/", "$getcontents", $look)) {
   echo "A match was found.<BR>$look";
} else {
   echo "A match was not found.";
}

?>
[/code]
Thats what i'v got so far but it just says a match was found array how would i desply the text after the or before were the string it found?
Link to comment
https://forums.phpfreaks.com/topic/4836-my-website-search/
Share on other sites

Something like


[code]$search = $_POST['s'];
$file="index.php";
$getcontents=file_get_contents($file);

if (false !== ($p = strpos ($getcontents, $search))) {

    $s = $p < 50 ? 0 : $p - 50;
    $e = $p + 60;

    $getcontents = substr($getcontents, $s, $e-$s);
    $getcontents = str_replace ($search,  "<SPAN style='background-color: red; color: white'>$search </SPAN>",  $getcontents);

    echo $getcontents;

}
else {
      echo "'$search' not found";
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/4836-my-website-search/#findComment-17190
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.