0

I have a undivided big String array which looks kind of like this:

VALUES(1,1,1,0,1,"TEst",0,"NAME","URL.de","RANDOM CRYPRIC STUFF 34","­RANDOM CRYPRIC STUFF34 ","test",1,2)
VALUES(1,1,1,0,1,"TEst",0,"NAME","URL.de","RANDOM CRYPRIC STUFF 34","­RANDOM CRYPRIC STUFF34 ","test",1,2)
VALUES(1,1,1,0,1,"TEst",0,"NAME","URL.de","RANDOM CRYPRIC STUFF 34","­RANDOM CRYPRIC STUFF34 ","test",1,2)

The Values are all diffrent and my PHP code looks like this to get the individual Values:

$backupdat = fopen("text.txt","r");
while(!feof($backupdat))
   {
   $found1.= strchr(fgets($backupdat,1024),"VALUES");
   }
fclose($backupdat);


$whatIWant = explode("VALUES(",$found1);

$User= explode(",",$whatIWant[1]);
enter code here
echo $User[10]."--";    
echo $User[11]."--";

Now I can browse all things with Values by changing the Array "WhatIWant[X]" and $User[x].

The thing is the "Random Cryptic Stuff can be everything all chars including , " and newRow. So my explode method gets the wrong input.

I'm now looking for some other way to get my values, the only thing fix in length is the Crypric stuff which is 34 chars long.

11
  • Must the data be saved in the format? Or could you use a conventional serialization method Commented Sep 2, 2014 at 10:59
  • One option is to explode as you are until the URL.de part, then count 34 chars past that to get random "crypric" stuff, then explode the rest as you were Commented Sep 2, 2014 at 10:59
  • @user574632 what do you mean the Data ? my two arrays or the Data i whant to split into pieces ? The Data Values(....) is fix i cant change that and the two arrays i used to get a quick methode for each individual value Commented Sep 2, 2014 at 11:04
  • @GEnGEr i mean the contents text.txt. If you can change how this is saved, json or phps serialize() would make retrieving values much easier Commented Sep 2, 2014 at 11:08
  • @user574632 nope the Text.txt is fix and not an option to change i need to work with the given stuff XD Commented Sep 2, 2014 at 11:11

1 Answer 1

1

I guess there is no clever way to do this. You could build your own parsing function, something like this:

$mystring = 'VALUES(1,1,1,0,1,"TEst",0,"NAME","URL.de","abcdefghijklmnopqrstuvwxyzabcdefgh","abcdefghijklmnopqrstuvwxyzabcdefgh","test",1,2)';

$stripped = substr($mystring, 7, strlen($mystring)-8);

function parsecol($string, &$index) {
  $val = '';
  if ($string[$index] == '"') {
    $limit = '"';
    $index++;
  } else {
    $limit = ',';
  }
  while (isset($string[$index]) && $string[$index] != $limit) {
    $val .= $string[$index];
    $index++;
  }
  if ($limit == '"') {
    $index++;
  }
  $index++;
  return $val;
}


$user = array();
$index = 0;
// parse first 9 columns
for ($i=0; $i<9; $i++){
  $user[] = parsecol($stripped, $index);
  var_dump($user);
}
// get the cryptic strings
$user[] = substr($stripped, $index+1, 34);
var_dump($user);
$index += 37; // pass 34 chars from crypt, 2 " and ,
$user[] = substr($stripped, $index+1, 34);
$index += 37;
for ($i=0; $i<3; $i++){
  $user[] = parsecol($stripped, $index);
}

var_dump($user);
Sign up to request clarification or add additional context in comments.

1 Comment

works like a charm, last problem is its not that easy to get $mystring because of two things. 1. VALUES( is somewhere in the Text.txt and 2. to get the whole line without getting cut of by the cryptic stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.