By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

split()

P: 44
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.         $q=$_POST['path'];
  4.  
  5.     $jaar=$_POST['years'];
  6.     $day=$_POST['day'];
  7.     //echo $day."<br />";
  8.     $month=$_POST['month'];
  9.     //echo $month."<br />";
  10.  
  11.     $co= $_POST['coAbr'];
  12.  
  13.     $num=$_POST['Number'];
  14.     $lineNumbers;
  15.     $lineNumber;
  16.  
  17.     if($_POST['Number'])
  18.  
  19.      {
  20.  
  21.          // open the file 
  22.  
  23.          $file =$q."file1.txt";
  24.  
  25.         // patterns to serch the file
  26.  
  27.         $preg = "\[$jaar\] $co  $num;";
  28.           //echo"the preg is:".$preg."<br />";
  29.         $data=stripslashes($preg);
  30.  
  31.         $lines = file($file); 
  32.  
  33.         $count;
  34.  
  35.         if ($lines)
  36.  
  37.             {
  38.  
  39.                 foreach($lines as $lineNumber) 
  40.  
  41.                 { 
  42.  
  43.                 $lineNumbers++;
  44.  
  45.                     $foundAMatch = preg_match("/$preg/i", $lineNumber, $matches, PREG_OFFSET_CAPTURE); 
  46.  
  47.                     if ($foundAMatch > 0) 
  48.  
  49.                     { 
  50.  
  51.  
  52.                        $found = $matches[0][0]; 
  53.  
  54.                    $count=$lineNumber;
  55.                  echo"The line number is : ".$lineNumber."<br />";
  56.                 echo"The court name is:".$_POST['courtAbr']."<br />";
  57.  
  58.                                echo"Line from the Registry.<br /> ";
  59.                 $words = split(" ", $lineNumber);
  60.                 echo "File name is : ".$words[0]."<br />";
  61.                 $lenth = strlen($words[0]) ;
  62.                 $lineNumber1= substr($lineNumber , $lenth);
  63.                 echo "The Line number one is : ".$lineNumber1."<br />";
  64.                 $words4 = split(")", $lineNumber1);
  65.                 $lenth4 = strlen($words4[0]) ;
  66.                 echo "the lenth :".$lenth4."<br />";
  67.                 echo "File name is : ".$words4[0]."<br />";
  68.  
  69.                 $lineNumber2= substr($lineNumber1 , $lenth4);
  70.                 echo "the line num :".$lineNumber2."<br >";
  71.                 $words5 = split(")", $lineNumber2);
  72.                 echo $words5[0]."<br />";        
  73.  
  74.                          }
  75.  
  76.           }
  77.  
  78.      }  
  79.       }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. ?>
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)
Oct 30 '08 #1
Share this Question
Share on Google+
2 Replies


Markus
Expert 5K+
P: 6,050
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.         $q=$_POST['path'];
  4.  
  5.     $jaar=$_POST['years'];
  6.     $day=$_POST['day'];
  7.     //echo $day."<br />";
  8.     $month=$_POST['month'];
  9.     //echo $month."<br />";
  10.  
  11.     $co= $_POST['coAbr'];
  12.  
  13.     $num=$_POST['Number'];
  14.     $lineNumbers;
  15.     $lineNumber;
  16.  
  17.     if($_POST['Number'])
  18.  
  19.      {
  20.  
  21.          // open the file 
  22.  
  23.          $file =$q."file1.txt";
  24.  
  25.         // patterns to serch the file
  26.  
  27.         $preg = "\[$jaar\] $co  $num;";
  28.           //echo"the preg is:".$preg."<br />";
  29.         $data=stripslashes($preg);
  30.  
  31.         $lines = file($file); 
  32.  
  33.         $count;
  34.  
  35.         if ($lines)
  36.  
  37.             {
  38.  
  39.                 foreach($lines as $lineNumber) 
  40.  
  41.                 { 
  42.  
  43.                 $lineNumbers++;
  44.  
  45.                     $foundAMatch = preg_match("/$preg/i", $lineNumber, $matches, PREG_OFFSET_CAPTURE); 
  46.  
  47.                     if ($foundAMatch > 0) 
  48.  
  49.                     { 
  50.  
  51.  
  52.                        $found = $matches[0][0]; 
  53.  
  54.                    $count=$lineNumber;
  55.                  echo"The line number is : ".$lineNumber."<br />";
  56.                 echo"The court name is:".$_POST['courtAbr']."<br />";
  57.  
  58.                                echo"Line from the Registry.<br /> ";
  59.                 $words = split(" ", $lineNumber);
  60.                 echo "File name is : ".$words[0]."<br />";
  61.                 $lenth = strlen($words[0]) ;
  62.                 $lineNumber1= substr($lineNumber , $lenth);
  63.                 echo "The Line number one is : ".$lineNumber1."<br />";
  64.                 $words4 = split(")", $lineNumber1);
  65.                 $lenth4 = strlen($words4[0]) ;
  66.                 echo "the lenth :".$lenth4."<br />";
  67.                 echo "File name is : ".$words4[0]."<br />";
  68.  
  69.                 $lineNumber2= substr($lineNumber1 , $lenth4);
  70.                 echo "the line num :".$lineNumber2."<br >";
  71.                 $words5 = split(")", $lineNumber2);
  72.                 echo $words5[0]."<br />";        
  73.  
  74.                          }
  75.  
  76.           }
  77.  
  78.      }  
  79.       }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. ?>
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.
Oct 30 '08 #2

nathj
Expert 100+
P: 938
My answer/suggestion is dependent on the string format being the same every time, though the length of each part can change.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.      $lcStartString = '"1980" (5 dec 1234) shadee codes (1234)' ;    
  3.      $lcOriginalString = $lcStartString ; // just in case you need the original
  4.      echo $lcStartString . '<br />' ;                     
  5.      $lcFirstLine = substr($lcStartString, 0, strpos($lcStartString, '"', 3)+1) ;
  6.      echo $lcFirstLine . '<br />' ;             
  7.      $lcStartString = str_replace($lcFirstLine, "", $lcStartString) ;
  8.      $lcSecondLine = substr($lcStartString, 0, strpos($lcStartString, ")") +1) ;
  9.      echo $lcSecondLine . '<br />' ;                                  
  10.      $lcStartString = str_replace($lcSecondLine, "", $lcStartString) ;
  11.      $lcThirdLine = substr($lcStartString, 0, strpos($lcStartString, "(")-1);  
  12.      echo $lcThirdLine . '<br />' ;
  13.      $lcStartString = str_replace($lcThirdLine, "", $lcStartString) ; 
  14.      $lcFourthLine = $lcStartString ;
  15.      echo $lcFourthLine . '<br />' ;
  16. ?>
  17.  
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
Oct 31 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.