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 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    91
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Problems finding a pattern in a line.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>";
    	}
    	
    }
    The pattern to be searched is like this:

    Code:
    "test1_WMA_40_v2_128kbps_48kHz_2"
    and the file contains these type of names as below:

    Code:
    settop decode -audio 0x1 -mpeg_type ASF -audio_type 0x86 /wma/L2/WMA_V4.0/test1_WMA_40_v2_128kbps_48kHz_2.asf	test1_WMA_40_v2_128kbps_48kHz_2	C:\Audio_Regress\recorded_files	90	48000


    Thanks,
    Ramki.

  • #2
    New Coder
    Join Date
    Mar 2008
    Location
    Lakeland, FL
    Posts
    39
    Thanks
    1
    Thanked 3 Times in 3 Posts
    I think you need to delimit the underscores when using regexp. you also need a slash in front and back of the pattern.

    PHP Code:

    $pattern 
    str_replace("_""\_"$stream_names[$d]);
    $pattern "/".$pattern."/"


  •  

    Posting Permissions

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