GEnGEr
GEnGEr

Reputation: 205

PHP get a specific String with random values

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.

Upvotes: 0

Views: 77

Answers (1)

Alfred_P
Alfred_P

Reputation: 792

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);

Upvotes: 1

Related Questions