Search File for Strings

Discussion in 'PHP' started by SiteBuyerUK, May 2, 2007.

  1. #1
    Hey,

    I would like to know how to open a txt file and search it using PHP and record every string between the following 2 strings:

    1. ><a href="/cool/of/
    2. /"><img src="/awesome.gif"

    Then create a new txt file with each string found on a new line
     
    SiteBuyerUK, May 2, 2007 IP
  2. Subikar

    Subikar Active Member

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    At first you saywhat you want to search from the text file content.
     
    Subikar, May 2, 2007 IP
  3. SiteBuyerUK

    SiteBuyerUK Peon

    Messages:
    253
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well inbetween those 2 strings are usernames, i need to list them all from the file by removing them from the source
     
    SiteBuyerUK, May 2, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $searchIn = file_get_contents('somefile.txt');
    preg_match_all('@><a href="/cool/of/(.*?)/"><img src="/awesome\.gif"@',$searchIn,$matches,PREG_SET_ORDER);
    $userNames = $matches[0];
    unset($matches);
    $saveAs = implode("\n\r",$userNames);
    file_put_contents('usernames.txt',$saveAs);
    PHP:
    Untested but along those lines.
     
    rodney88, May 2, 2007 IP
  5. SiteBuyerUK

    SiteBuyerUK Peon

    Messages:
    253
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Fatal error: Call to undefined function: file_put_contents()

    Doesnt quite work using PHP 4.4.3
     
    SiteBuyerUK, May 2, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    if(!function_exists("file_put_contents"))
    {
    function file_put_contents( $file, $data )
    {
    $handle = fopen( $file, "w+" );
    $write = fwrite( $handle, $data );
    fclose( $handle );
    return $write ;
    }
    }
     
    krakjoe, May 2, 2007 IP
  7. SiteBuyerUK

    SiteBuyerUK Peon

    Messages:
    253
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Add that to the existing code? Dont i need to define file etc?
     
    SiteBuyerUK, May 2, 2007 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    add it to ( above ) the code
     
    krakjoe, May 2, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    
    <?php
    function put_strings( $infile, $outfile )
    {
    	if(!($search = file_get_contents( $infile ))):
    		print("Cannot retrieve $infile<Br/>\n");
    		return false;
    	endif;
    	preg_match_all( '#<a href="/cool/of/(.*?[^/])/">#si', $search, $found );
    	if( $found ):
    		$handle = fopen( $outfile, "w+" );
    		if( !$handle ):
    			print( "Cannot open $outfile for writing<br/>\n" );
    			return false;
    		endif;
    		$write = fwrite( $handle, implode("\r\n", $found[1] ) );
    		fclose( $handle );
    	endif;
    	
    	return !$write ? false : count( $found[1] ) ;
    }
    if( !put_strings( 'test.php', 'out.txt' ) ):
    	echo "CAN'T<br/>\n";
    else:
    	echo "DONE<br/>\n";
    endif;
    ?>
    
    PHP:
     
    krakjoe, May 2, 2007 IP
    SiteBuyerUK likes this.
  10. SiteBuyerUK

    SiteBuyerUK Peon

    Messages:
    253
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Worked Great! rep given
     
    SiteBuyerUK, May 2, 2007 IP