PHP对格式化字符串中的元素进行计数

PHP对格式化字符串中的元素进行计数,php,arrays,string,Php,Arrays,String,这一天来一直困扰着我,所以我需要一些帮助。 在给定字符串中,如以下所示: $str = 'a:1:{s:8:"post_tag";a:5:{ i:460;a:5:{s:11:"wpseo_title";s:8:"demodemo";s:10:"wpseo_desc";s:8:"demodemo";s:13:"wpseo_bctitle";s:8:"demodemo";s:13:"wpseo_noindex";s:7:"default";s:21:"wpseo_sitemap_include";s

这一天来一直困扰着我,所以我需要一些帮助。 在给定字符串中,如以下所示:

$str = 'a:1:{s:8:"post_tag";a:5:{
i:460;a:5:{s:11:"wpseo_title";s:8:"demodemo";s:10:"wpseo_desc";s:8:"demodemo";s:13:"wpseo_bctitle";s:8:"demodemo";s:13:"wpseo_noindex";s:7:"default";s:21:"wpseo_sitemap_include";s:1:"-";}
i:459;a:5:{s:11:"wpseo_title";s:8:"testtest";s:10:"wpseo_desc";s:8:"testtest";s:13:"wpseo_bctitle";s:8:"testtest";s:13:"wpseo_noindex";s:7:"default";s:21:"wpseo_sitemap_include";s:1:"-";}
i:462;a:5:{s:11:"wpseo_title";s:7:"bikey77";s:10:"wpseo_desc";s:7:"bikey77";s:13:"wpseo_bctitle";s:7:"bikey77";s:13:"wpseo_noindex";s:7:"default";s:21:"wpseo_sitemap_include";s:1:"-";}
i:463;a:5:{s:11:"wpseo_title";s:7:"dimitra";s:10:"wpseo_desc";s:7:"dimitra";s:13:"wpseo_bctitle";s:7:"dimitra";s:13:"wpseo_noindex";s:7:"default";s:21:"wpseo_sitemap_include";s:1:"-";}
i:464;a:5:{s:11:"wpseo_title";s:8:"dimitra1";s:10:"wpseo_desc";s:8:"dimitra1";s:13:"wpseo_bctitle";s:8:"dimitra1";s:13:"wpseo_noindex";s:7:"default";s:21:"wpseo_sitemap_include";s:1:"-";}
}}';
如何计算以I:XXX开头的子字符串的数量。。。以…结尾;}

子字符串示例:


i:460;a:5:s:11:“wpseo_标题”;s:8:“蠕形螨”;s:10:“wpseo_desc”;s:8:“蠕形螨”;s:13:“wpseo_bctitle”;s:8:“蠕形螨”;s:13:“wpseo_noindex”;s:7:“违约”;s:21:“wpseo_站点地图_include”;s:1:“-”;}

看起来您正在处理一个序列化数组,所以

$data = unserialize($str);
echo count($data['post_tag']);

这是序列化数据,请取消序列化

$arr = unserialize($str);
echo count($arr['post_tag']);

你能解开字符串并数一数行吗?我很惊讶这个问题与感谢否决票的问题如此相似。也许你也可以提出一个解决方案?或者您只是出于聪明?已尝试取消序列化,返回0。另外,它可能应该是count($data['wpseo_title']),因为post_标记在字符串中只出现一次。@bikey77
post_标记
包含5个
wpseo_title
这就是为什么
count($arr['post_标记])
@bikey77这很奇怪,因为我在1分钟前再次尝试,结果返回0。很明显,我做错了什么,所以我会接受你的答案为正确答案(这是第一个答案,尽管约格什也给出了同样的答案)。非常感谢。@bikey77您的序列化数据可能包含换行符,这会使序列化格式无效。请确保您没有通过插入换行符或其他任何方式更改序列化数据。我只接受deceze的答案作为正确答案,因为他在您之前回答了,尽管他们完全相同。非常感谢。