Jump to content

Php String Search


Deanznet

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.