Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Problems finding a pattern in file. Please help.

    Hi,
    I need to search a pattern in each line of a file and if that pattern found then it should extract that complete line from the file and write it to another file. I've written the code but it not working!Please help. $Data is getting the data. $pattern is present in that file which i checked it out manually.Is my searching correct?

    Code:
    $handle = fopen($Filename, 'r');
    $File1 = "Automation_streams.txt";
    $Handle1 = fopen($File1, 'w');
    $d=0;
    
    while(!feof($handle))
    { 
    	$Data = fgets($handle,1024);
    	$pattern = $stream_names[$d];
    	if(preg_match($pattern,$Data)!=0)
    	{
    		echo "Matched data is $Data<br>";
    		fwrite($Handle1,$Data);
    		$d++;
    		print "<p>";
    	}
    	
    }
    Thanks,
    Ramki.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    echo $pattern and echo $Data and show us those values.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Without looking at anything else, the array you want to use for the matched data should be the third argument in the preg_match() function, and you're trying to use $Data inside of the if switch, which should be the subject. If you want more specific help you need to post your pattern, and an example you're trying to match.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •