split()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ogo796
    New Member
    • Jan 2008
    • 44

    split()

    Hi guys am having a problem with a split(),i retrieve line from the text file
    and i wanna split that line.i manage to split two words but splitting the string
    enclosed on brackets it seems to be a problem to me. can someone look at my code and please help me maybe i am missing something.
    Code:
    <?php
    
            $q=$_POST['path'];
    
    	$jaar=$_POST['years'];
    	$day=$_POST['day'];
    	//echo $day."<br />";
    	$month=$_POST['month'];
    	//echo $month."<br />";
    
    	$co= $_POST['coAbr'];
    
    	$num=$_POST['Number'];
    	$lineNumbers;
    	$lineNumber;
    
    	if($_POST['Number'])
    
    	 {
    
    	 	// open the file 
    
    	 	$file =$q."file1.txt";
    
    		// patterns to serch the file
    
    		$preg = "\[$jaar\] $co  $num;";
    	  	//echo"the preg is:".$preg."<br />";
    		$data=stripslashes($preg);
    
    		$lines = file($file); 
    
    		$count;
    
    		if ($lines)
    
    			{
    
        			foreach($lines as $lineNumber) 
    
        			{ 
    
    				$lineNumbers++;
    
            			$foundAMatch = preg_match("/$preg/i", $lineNumber, $matches, PREG_OFFSET_CAPTURE); 
    
            			if ($foundAMatch > 0) 
    
            			{ 
    					
    
               			$found = $matches[0][0]; 
    
    		   		$count=$lineNumber;
     				echo"The line number is : ".$lineNumber."<br />";
    				echo"The court name is:".$_POST['courtAbr']."<br />";
    
                           		echo"Line from the Registry.<br /> ";
    				$words = split(" ", $lineNumber);
    				echo "File name is : ".$words[0]."<br />";
    				$lenth = strlen($words[0]) ;
    				$lineNumber1= substr($lineNumber , $lenth);
    				echo "The Line number one is : ".$lineNumber1."<br />";
    				$words4 = split(")", $lineNumber1);
    				$lenth4 = strlen($words4[0]) ;
    				echo "the lenth :".$lenth4."<br />";
    				echo "File name is : ".$words4[0]."<br />";
    						
    				$lineNumber2= substr($lineNumber1 , $lenth4);
    				echo "the line num :".$lineNumber2."<br >";
    				$words5 = split(")", $lineNumber2);
    				echo $words5[0]."<br />";		
    
                        	 }
    
    		  }
    
    	 }  
          }
    	
    	
    	
    
    
    
    
    ?>
    the below is the one that i wanna split()
    "1980" (5 dec 1234) shadee codes (1234)

    i am looking to achive the following results
    "1980"
    (5 dec 1234)
    shadee codes
    (1234)
    Last edited by Markus; Oct 30 '08, 02:13 PM. Reason: added # tags
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by ogo796
    Hi guys am having a problem with a split(),i retrieve line from the text file
    and i wanna split that line.i manage to split two words but splitting the string
    enclosed on brackets it seems to be a problem to me. can someone look at my code and please help me maybe i am missing something.
    Code:
    <?php
    
            $q=$_POST['path'];
    
    	$jaar=$_POST['years'];
    	$day=$_POST['day'];
    	//echo $day."<br />";
    	$month=$_POST['month'];
    	//echo $month."<br />";
    
    	$co= $_POST['coAbr'];
    
    	$num=$_POST['Number'];
    	$lineNumbers;
    	$lineNumber;
    
    	if($_POST['Number'])
    
    	 {
    
    	 	// open the file 
    
    	 	$file =$q."file1.txt";
    
    		// patterns to serch the file
    
    		$preg = "\[$jaar\] $co  $num;";
    	  	//echo"the preg is:".$preg."<br />";
    		$data=stripslashes($preg);
    
    		$lines = file($file); 
    
    		$count;
    
    		if ($lines)
    
    			{
    
        			foreach($lines as $lineNumber) 
    
        			{ 
    
    				$lineNumbers++;
    
            			$foundAMatch = preg_match("/$preg/i", $lineNumber, $matches, PREG_OFFSET_CAPTURE); 
    
            			if ($foundAMatch > 0) 
    
            			{ 
    					
    
               			$found = $matches[0][0]; 
    
    		   		$count=$lineNumber;
     				echo"The line number is : ".$lineNumber."<br />";
    				echo"The court name is:".$_POST['courtAbr']."<br />";
    
                           		echo"Line from the Registry.<br /> ";
    				$words = split(" ", $lineNumber);
    				echo "File name is : ".$words[0]."<br />";
    				$lenth = strlen($words[0]) ;
    				$lineNumber1= substr($lineNumber , $lenth);
    				echo "The Line number one is : ".$lineNumber1."<br />";
    				$words4 = split(")", $lineNumber1);
    				$lenth4 = strlen($words4[0]) ;
    				echo "the lenth :".$lenth4."<br />";
    				echo "File name is : ".$words4[0]."<br />";
    						
    				$lineNumber2= substr($lineNumber1 , $lenth4);
    				echo "the line num :".$lineNumber2."<br >";
    				$words5 = split(")", $lineNumber2);
    				echo $words5[0]."<br />";		
    
                        	 }
    
    		  }
    
    	 }  
          }
    	
    	
    	
    
    
    
    
    ?>
    the below is the one that i wanna split()
    "1980" (5 dec 1234) shadee codes (1234)

    i am looking to achive the following results
    "1980"
    (5 dec 1234)
    shadee codes
    (1234)
    ogo796, you have been warned previously about posting code without using code tags. As you have done so again, you receive you second formal warning. Carry on doing so and you will receive a ban of some kind.

    Markus.

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      My answer/suggestion is dependent on the string format being the same every time, though the length of each part can change.

      Code:
      <?php
      	 $lcStartString = '"1980" (5 dec 1234) shadee codes (1234)' ;	
      	 $lcOriginalString = $lcStartString ; // just in case you need the original
      	 echo $lcStartString . '<br />' ;			 		
      	 $lcFirstLine = substr($lcStartString, 0, strpos($lcStartString, '"', 3)+1) ;
      	 echo $lcFirstLine . '<br />' ;			 
      	 $lcStartString = str_replace($lcFirstLine, "", $lcStartString) ;
      	 $lcSecondLine = substr($lcStartString, 0, strpos($lcStartString, ")") +1) ;
      	 echo $lcSecondLine . '<br />' ;			 					 
      	 $lcStartString = str_replace($lcSecondLine, "", $lcStartString) ;
      	 $lcThirdLine = substr($lcStartString, 0, strpos($lcStartString, "(")-1);  
      	 echo $lcThirdLine . '<br />' ;
      	 $lcStartString = str_replace($lcThirdLine, "", $lcStartString) ; 
      	 $lcFourthLine = $lcStartString ;
      	 echo $lcFourthLine . '<br />' ;
      ?>
      I hope this helps you out. it's fairly obvious what is going on here but if you get stuck or this isn't the right idea then post back - using code tags - and I'll try to help out some more.

      Cheers
      nathj

      Comment

      Working...