2013-10-04 49 views
1

包裹的两点之间的字符数确定这将是奇怪的,但我需要它PHP - 获得报价

我想获得字符计数的代码某些特定报价之间的庞大阵容”基本相符。我需要能够在年底获得在开始第三报价与5报价之间的一切。

所以这里有一个例子

a:2:{s:10:"categories";s:5758:"...........";s:5:"posts";s:6:"a:0:{}";} 

我需要知道的字符数是所有的东西实际上有代码代替那些时期。

由于有11个时期,所以我的人物数量将是11.唯一一致的是这里的引号,所以我需要根据这一点。

任何帮助都会很棒。

这是我的代码。我基本上是创建代码并添加一些自定义标签。我试图序列化代码之前,我反序列化它,但似乎没有工作。

<? 
$thisisit .= 'a:2:{s:10:"categories";s:5481:"a:40:{'; 
include('connect.php'); 

$sql = "SELECT * FROM wp_terms ORDER BY term_id ASC LIMIT 40"; 
$result = mysql_query($sql); 
$count = 0; 
while($row = mysql_fetch_array($result)) { 
    $name = $row['name']; 
    $charactercount = strlen($name); 
    $term_id = $row['term_id']; 
    $thisisit .= 'i:'.$count.';a:2:{s:11:"filter_name";s:20:"add_keyword_category";s:11:"filter_args";a:7:{s:12:"filter_value";s:'.$charactercount.':"'.strtolower($name).'";s:19:"filter_search_title";s:1:"1";s:21:"filter_search_excerpt";i:0;s:21:"filter_search_content";s:1:"1";s:21:"faf_filter_categories";a:1:{i:4;s:3:"'.$term_id.'";}s:17:"filter_match_word";i:0;s:17:"filter_match_case";i:0;}}'; 
    //echo "<br><br>"; 
    $count++; 
} 
$thisisit .= '}";s:5:"posts";s:6:"a:0:{}";}'; 

$array = unserialize($thisisit); 
echo strlen($array['categories']); 
?> 
+0

序列化的数据。该字符串长5758个字符。 – Arjan

回答

2

实际上这个数据看起来是序列化的。正确的解决方案是使用php函数unserialize

然后,给你的结构,就知道该元素的长度:

strlen(unserialize($data)['categories']); 

如果运行老的PHP,你需要将结果保存在临时变量:

$array = unserialize($data); 
echo strlen($array['categories']); 

如果您的序列化数据已损坏(如“未收到正确执行serialize”),从您的示例看来,我们可以返回到您的原始任务:

get everything between the 3rd quote in the beginning and the 5th quote at the end

实现这一最简单的方法是:

implode("'", array_slice(explode("'", $data), 3, -5)); 
+0

不应该是strlen(反序列化($ data ['categories'])); ?我改变它到这个,它返回0 –

+0

不,它不应该。你首先反序列化整个字符串并获得一个数组。然后你从该数组中取出类别。 – Denis

+0

您的版本返回分析错误:语法错误,意外的'['在/home/content/g/u/i/...../html/....../test.php在线xx –