Jump to content

need to get some date from file table or excel


Stream

Recommended Posts

I've got a file like bellow (the date might be about 5000) RESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSL PARAMETERS:M'35611523535 , , M' , GGGHJ 89098 HU (1) State HJUK 18292 (this string I should escape)GGGHJ 45441 HU (1) Lertd HJUK 157346 (this string I should escape) RESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSLPARAMETERS:M'35611472097 , , M' , and I should get some date from it like here: DSL -----------number1- 8080 523535 (I should get it from M'35611523535) and then echo it to table or exel file. <?php$filename = 'test.txt';$fp = fopen($filename, 'w'); Here I think i should create some thing like this for i= 0 to count (ammount of string) echo to table fclose($fp);?> Thanks in advance

Edited by Stream
Link to comment
Share on other sites

RESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSL PARAMETERS:M'35611523535 , , M' , From this part RESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSL I should get (DSL 1- 8080)From this part M'35611523535 , , M' , (523535 meaning the last seven number)

Link to comment
Share on other sites

From this part RESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSL I should get (DSL 1- 8080)
What are the rules though? Do you want to tell it to look for "CPS", and then get everything after that until it finds a "("? Should it look for "DSL" and then get everything after that which is either a digit, a space, or a hyphen? There needs to be a specific set of rules that you're telling the computer to follow. For this:
From this part M'35611523535 , , M' , (523535 meaning the last seven number)
If you're looking for the last 7 digits of the number then one way would be to extract any string made of numbers, which you can do with a regular expression, and then get the last 7 characters using the substr function. You could also use explode to split the line up on commas, get the first item and trim it, and then use substr. http://www.php.net/m...tion.substr.phphttp://www.php.net/m...ion.explode.phphttp://www.php.net/m....preg-match.phphttp://www.php.net/manual/en/function.trim.php
Link to comment
Share on other sites

thanks for recomended tutorialbellow I'd like to show what i needRESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSL PARAMETERS:M'35611523535 , , M' ,RESOURCE: CPS DSL 1- 6478 ( 1) STATE: DSL PARAMETERS:M'33333333333 , , M' ,RESOURCE: CPS DSL 1- 8036 ( 1) STATE: DSL PARAMETERS:M'35611523400 , , M' , For me its enough to get date after CPS for example:DSL 1- 8036DSL 1- 8080DSL 1- 8036

If you're looking for the last 7 digits of the number
Yes ! And http://www.php.net/m...tion.substr.php exactly what i need I think i should build something like this
<?php$filename = 'test.txt';$fp = fopen($filename, 'w'); Here I think i should create some thing like this for i= 0 to count (ammount of string) echo substr('abcdef', 1, 3);  // bcd in my  case echo substr('M'35611523400', 6, 7);  // 523400 echo to table or excel = DSL 1- 8080echo to table or excel = 523400 fclose($fp);?>

Edited by Stream
Link to comment
Share on other sites

For me its enough to get date after CPS for example:DSL 1- 8036DSL 1- 8080DSL 1- 8036
If those are always going to be the same length, and the strings always start with the same thing, then it's fastest to use substr to get that value also. If the string might contain more or less text at the start, but what you're looking for always starts with "DSL" and will always be the same length, then you could use something like strpos to figure out where "DSL" is in the string, and then use that position with substr to get the rest of the text. If the string could be any size and may not include "DSL" then you'll probably need to use a regular expression. You can also use explode to split the string into words and then check each word.
Link to comment
Share on other sites

You can use the file function to get the lines of the file in an array: http://www.php.net/manual/en/function.file.php That will put each line in the array as its own element. Then you can loop through the array using a for or foreach loop and run the code you want on each line. Check the PHP manual or the PHP tutorials on this site if you want examples of loops.

Link to comment
Share on other sites

I built an array with what I need to remove and then using str_replace I remove it but I'd like to use IF statement like bellowif (!strpbrk($phrase, "RESOURCE:"));here should be something like if just the string has "RESOURCE:" or M'3561should be done str_replace and echo it.But it does't work.

$file=fopen("test2.txt","r");$numbers = array("RESOURCE: ", "CPS ", "( 1)","STATE: EQ ", "PARAMETERS",":","M'3561",", ","K'","( 0) STATE NE ","READ REPORT","0710138"," 29 ","0029"," 03-03-19"," 1745");//$words = array("", "", "");while (!feof($file)){// $phrase = "RESOURCE: CPS DSL 1- 8080 ";$phrase = fgets($file);if (!strpbrk($phrase, "RESOURCE:"));{here should be something like if just the string has "RESOURCE:" or M'3561should be done str_replace and echo it$change = str_replace($numbers, "$words", $phrase);}Echo $change ."<br>";}echo count($phrase);fclose($file);

Could you help me please to solve the problemThanks in advance

Link to comment
Share on other sites

Remove the semicolon after the if statement, that is terminating the if statement. You can use arrays with str_replace but it's a problem if you put the array in quotes like you have, you're converting the array to a string when you do that. It doesn't sound like strpbrk is the function you want to use though. You're checking if the phrase contains an "R", or an "E", or an "S", etc, not the word "RESOURCE". Maybe you want to use strpos instead. http://www.php.net/manual/en/function.strpbrk.phphttp://www.php.net/manual/en/function.strpos.php

Link to comment
Share on other sites

I could get something like bellow which gets the date I need but there is something goes wrong it's echos the date which I am trying to exclude using if statment. Am I incorrect in building If statment

<?php$file=fopen("test2.txt","r");$numbers = array("RESOURCE: ","PARAMETERS:", "DSL  ","( 1)","STATE: SL ","( 0)  STATE: VT","( 2) ","[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif][size=3]M'35611[/size][/font][/color]",",","K","'");//$words = array("", "", "");while (!feof($file)){$phrase = fgets($file);$phrasenum = fgets($file);  $pos = strpos($phrase, "RESOURCE:") && $posnum = strpos($phrasenum , "M'35611");if (($pos!==false) || ($posnum!==false)){				   $change = str_replace($numbers, "", $phrase);$changenum = str_replace($numbers, "",$phrasenum);}  Echo $change . $changenum ."<br>"; }fclose($file);?>

Thanks in advance

Edited by Stream
Link to comment
Share on other sites

What are you trying to do with this: $pos = strpos($phrase, "RESOURCE:") && $posnum = strpos($phrasenum , "M'35611");
I am trying to see if there is in the string "RESOURCE:" and "M'35611" the code should do replace ("RESOURCE: CPS" DSL 1- 8080 "( 1) STATE: DSL" and echo "DSL 1- 8080") && ("M'35611"523535 " , , M' ," and echo 523535) else it should bypass the string. Edited by Stream
Link to comment
Share on other sites

If you want $pos to be the position of "RESOURCE:" and $posnum to be the position of the other string then you need to separate those 2 lines. When you use the && operator you are doing something completely different. You're setting $posnum to the correct value first, but $pos will be either true or false because && is a boolean operator which is telling PHP to store the boolean result of that expression. It would be the same as this: $pos = (strpos($phrase, "RESOURCE:") && $posnum = strpos($phrasenum , "M'35611")); $posnum gets set to the return value from strpos, and $pos gets set to the boolean result of that AND expression. If you want each variable to have the return value from strpos then those need to go on their own lines, that's not what the && operator is for.

Link to comment
Share on other sites

do you mean i should do some thing like this

while (!feof($file)){$phrase = fgets($file);$phrasenum = fgets($file);//  $pos = (strpos($phrase, "RESOURCE:") and $posnum = strpos($phrasenum , "M'67832"));$pos = strpos($phrase, "RESOURCE:");$posnum = strpos($phrasenum , "M'67832");if (($pos!==false) || ($posnum!==false)){				  $change = str_replace($numbers, "", $phrase);$changenum = str_replace($numbers, "",$phrasenum);}  Echo $change . $changenum ."<br>"; 

Edited by Stream
Link to comment
Share on other sites

but this way it prints 6 times the same date in the string of " RESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSL " 6 times and just then goes to the other string and again for 6 times of "RESOURCE:" or "M'35611".

Edited by Stream
Link to comment
Share on other sites

<?php$file=fopen("test2.txt","r");$numbers = array("RESOURCE: ","PARAMETERS:", "CPS  ","( 1)","STATE: EQ ","( 0)  STATE: NE","( 2) ","M'35611",",","M","'");//$words = array("", "", "");while (!feof($file)){$phrase = fgets($file);$phrasenum = fgets($file);$pos = strpos($phrase, "RESOURCE:");$posnum = strpos($phrasenum , "M'35611");if (($pos!==false) || ($posnum!==false)){				   $change = str_replace($numbers, "", $phrase);$changenum = str_replace($numbers, "",$phrasenum);}  Echo $change . $changenum ."<br>";}fclose($file);?>

File look's like this (about 2000) : RESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSL PARAMETERS:M'35611523535 , , M' , GGGHJ 89098 HU (1) State HJUK 18292 (this string I should escape)GGGHJ 45441 HU (1) Lertd HJUK 157346 (this string I should escape)RESOURCE: CPS DSL 1- 8081 ( 1) STATE: DSLPARAMETERS:M'35611472097 , , M' , GGGHJ 89098 HU (1) State HJUK 18292 (this string I should escape)GGGHJ 45441 HU (1) Lertd HJUK 157346 (this string I should escape)RESOURCE: CPS DSL 1- 8082 ( 1) STATE: DSLPARAMETERS:M'35611928613 , , M' , GGGHJ 89098 HU (1) State HJUK 18292 (this string I should escape)GGGHJ 45441 HU (1) Lertd HJUK 157346 (this string I should escape)RESOURCE: CPS DSL 1- 8083 ( 1) STATE: DSLPARAMETERS:M'35611928296 , , M' , Thanks a lot

Link to comment
Share on other sites

I guess it may help to go through the code and explain what it's doing. You open the file, and define your list of strings to get rid of, then start the while loop. Each time through the loop you get 2 lines. So, the first time through you would get this line in $phrase: RESOURCE: CPS DSL 1- 8080 ( 1) STATE: DSL and this line in $phrasenum: PARAMETERS: Then you check if "RESOURCE:" appears in $phrase, or "M'35611" appears in $phrasenum, and if so then you remove all of the strings you specified and save the new values in $change and $changenum. You only set those 2 variables if the if statement passes, but you always print them. So if you go through the loop and don't do any replacements because the if statement doesn't pass, you're going to print out the variables that were set last time. That's why it's repeating. Then the next time through the loop it's going to get the next 2 lines. So the second time through $phrase will have this: M'35611523535 , , M' , and $phrasenum will be an empty line. The next time through, $phrase has this: GGGHJ 89098 HU (1) State HJUK 18292 (this string I should escape) and $phrasenum has this: GGGHJ 45441 HU (1) Lertd HJUK 157346 (this string I should escape) The next time through, both of them have empty lines, etc.

  • Like 1
Link to comment
Share on other sites

I think i should change it

if (($pos!==false) || ($posnum!==false)){				   $change = str_replace($numbers, "", $phrase);$changenum = str_replace($numbers, "",$phrasenum);}

to this one

if (($pos!==false) || ($posnum!==false)){                   $change = str_replace($numbers, "", $phrase);  $changenum = str_replace($numbers, "",$phrasenum);  Else$change = "";$changenum ="";}

Link to comment
Share on other sites

You'll need to fix the brackets there, but I guess that would be one way. I would set it up so that instead of getting only 2 lines at a time, I would get 6 or 7 or however many there are in that repeating sequence. You only need to process a few of those lines, some of them are empty, but each loop through the file should grab an entire "chunk" of the data, however big that chunk is.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...