PHP获取具有随机值的特定String

时间:2014-09-02 10:57:04

标签: php

我有一个不可分割的大字符串数组,看起来像这样:

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)

值都是不同的,我的PHP代码看起来像这样得到个别值:

$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]."--";

现在我可以通过更改数组“WhatIWant [X]”和$ User [x]来浏览所有值。

事情是“随机隐秘的东西可以包括所有的所有字符”和newRow。 所以我的爆炸方法得到了错误的输入。

我现在正在寻找其他方法来获取我的价值观,唯一可以解决的问题是Crypric的东西是34个字符长。

1 个答案:

答案 0 :(得分:1)

我想没有聪明的方法可以做到这一点。您可以构建自己的解析函数,如下所示:

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