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! Link to comment https://forums.phpfreaks.com/topic/279286-php-string-search-so-confused-help/ Share on other sites More sharing options...
doddsey_65 Posted June 18, 2013 Share Posted June 18, 2013 Assuming that it is always 10 characters after the url you could try something like: $string = '<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'; $string = str_replace('\/', '/', $string); preg_match_all('|https\:\/\/superman.com\/([a-z0-9]{10})|i', $string, $matches); die(var_dump($matches)); That would give you array (size=2) 0 => array (size=3) 0 => string 'https://superman.com/Jmd8KKtjIj' (length=31) 1 => string 'https://superman.com/Jmd8KKtjIj' (length=31) 2 => string 'https://superman.com/Jmd8KKtjIj' (length=31) 1 => array (size=3) 0 => string 'Jmd8KKtjIj' (length=10) 1 => string 'Jmd8KKtjIj' (length=10) 2 => string 'Jmd8KKtjIj' (length=10) Link to comment https://forums.phpfreaks.com/topic/279286-php-string-search-so-confused-help/#findComment-1436531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.