Deanznet Posted June 17, 2013 Share Posted June 17, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/ Share on other sites More sharing options...
Deanznet Posted June 17, 2013 Author Share Posted June 17, 2013 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436491 Share on other sites More sharing options...
ginerjm Posted June 17, 2013 Share Posted June 17, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436497 Share on other sites More sharing options...
Deanznet Posted June 18, 2013 Author Share Posted June 18, 2013 (edited) The $search = 'https:\/\/superman.com\/'; Is Defining what its searching for. Im confused on how to capture the results.. Edited June 18, 2013 by Deanznet Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436506 Share on other sites More sharing options...
litebearer Posted June 18, 2013 Share Posted June 18, 2013 1. presumes lines in file.txt terminate with \n2. 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436508 Share on other sites More sharing options...
Deanznet Posted June 18, 2013 Author Share Posted June 18, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436515 Share on other sites More sharing options...
litebearer Posted June 18, 2013 Share Posted June 18, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436518 Share on other sites More sharing options...
Deanznet Posted June 18, 2013 Author Share Posted June 18, 2013 Hey! THAT WORKS! But one last question! And ill actually be done! Lets say i wanted to Only read maybe 30 LINES of that file and grab that info within 30 LINES. How can i do that ? Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436522 Share on other sites More sharing options...
litebearer Posted June 18, 2013 Share Posted June 18, 2013 Untested... Put this right after $lines = file('file.txt'); $c = count($lines); if($c>30) { $lines = array_slice($lines, 0, 30); } Quote Link to comment https://forums.phpfreaks.com/topic/279277-php-string-search/#findComment-1436597 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.