Jump to content

Recommended Posts

Hey!

 

Need some help with my string search!

 

 

I have been working on this script for a while but im STUCK!

 

I have a FILE.txt

 

Inside it has a bunch of random STUFF EXAMPLE:

 

 

 

<html><stuff>dsadfsdasfasfasfasfsafasfasfasf superman was awsome hehe https:\/\/superman.com\/Jmd8KKtjIj <html><stuff>dsadfsdasfasfasfasfsafasfasfasf superman was awsome hehe https:\/\/superman.com\/Jmd8KKtjIj <html><stuff>dsadfsdasfasfasfasfsafasfasfasf superman was awsome hehe https:\/\/superman.com\/Jmd8KKtjIj

 

 

 

 

I made a script like this.

<?php 

$search = 'https:\/\/superman.com\/'; 
// Read from file 
$lines = file('file.txt'); 
echo"<html><head><title>SEARCH RESULTS FOR: $search</title></head><body>";

foreach($lines as $line) 

{ 
// Check if the line contains the string we're looking for, and print if it does 

if(stristr($line,$search))  // case insensitive
    
echo "<font face='Arial'> $line </font><hr>"; 
} 

?>

But it actually echos the whole LINE! PLUS i need it to actually grab 10 characters after the search string so i actually need it just to grab https:\/\/superman.com\/Jmd8KKtjIj - In THIS CASE it would of echoed it 3 TIMES since its 3 of them in their.

 

Also any way i can FILTER out the extra slants it puts in ? https:\/\/superman.com\/Jmd8KKtjIj is suppose to be https://superman.com/Jmd8KKtjIj

 

Thanks!

 

 

Link to comment
https://forums.phpfreaks.com/topic/279277-php-string-search/
Share on other sites

Don't understand at all what your $search string is doing.

 

As for showing the whole line - of course it does.  What would make it NOT do that?  Your run a stristr but you don't capture the result and don't use that result to limit the output.

Link to comment
https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436497
Share on other sites

1. presumes lines in file.txt terminate with \n
2. presumes $needle only appears once in a line

<?php
$needle = 'https:\/\/superman.com\/';
$lines = file('file.txt');
echo"<html><head><title>SEARCH RESULTS FOR: $needle</title></head><body>";

foreach($lines as $line){
  if(stristr($line,$needle)){
    $temp_array1 = explode($needle, $line);
    if(strlen($temp_array1[0])>10){ $value = substr($dtemp_array1[0], -10) . $needle; }else{ $value = $temp_array1[0] . $needle; }
    if(strlen($temp_array1[1])>10){ $tail = substr($dtemp_array1[1], 0, 10); }else{ $tail = $temp_array1[1];}
    echo "<font face='Arial'>" . $value . $tail . "</font><hr>";
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436508
Share on other sites

1. presumes lines in file.txt terminate with \n

2. presumes $needle only appears once in a line

 

<?php
$needle = 'https:\/\/superman.com\/';
$lines = file('file.txt');
echo"<html><head><title>SEARCH RESULTS FOR: $needle</title></head><body>";

foreach($lines as $line){
  if(stristr($line,$needle)){
    $temp_array1 = explode($needle, $line);
    if(strlen($temp_array1[0])>10){ $value = substr($dtemp_array1[0], -10) . $needle; }else{ $value = $temp_array1[0] . $needle; }
    if(strlen($temp_array1[1])>10){ $tail = substr($dtemp_array1[1], 0, 10); }else{ $tail = $temp_array1[1];}
    echo "<font face='Arial'>" . $value . $tail . "</font><hr>";
}

?>

 

Hey Okay That Works.. But my next question is after ttps:\/\/superman.com\/ their is 10 Characters such as ttps:\/\/superman.com\/1234567891

 

How can i also get those?

Link to comment
https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436515
Share on other sites

My typo...

 

this:

    if(strlen($temp_array1[0])>10){ $value = substr($dtemp_array1[0], -10) . $needle; }else{ $value = $temp_array1[0] . $needle; }

    if(strlen($temp_array1[1])>10){ $tail = substr($dtemp_array1[1], 0, 10); }else{ $tail = $temp_array1[1];}

 

should be:

    if(strlen($temp_array1[0])>10){ $value = substr($temp_array1[0], -10) . $needle; }else{ $value = $temp_array1[0] . $needle; }

    if(strlen($temp_array1[1])>10){ $tail = substr($temp_array1[1], 0, 10); }else{ $tail = $temp_array1[1];}

 

$tail will contain any text that follows your search up to 10 characters

 

Link to comment
https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436518
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.