Jump to content

Php String Search.. So Confused Help


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/279286-php-string-search-so-confused-help/
Share on other sites

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)

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.