2013-10-04 105 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 –