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
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)
Edited by doddsey_65
Link to comment
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.