dongshou6041 2012-02-17 08:13
浏览 112
已采纳

从字符串中提取PHP文本

I have the following string: a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}

What I need to do is extract number 48 from it, in this case. This number could have any number of digits. How would I go about doing this?

  • 写回答

5条回答 默认 最新

  • doute7910 2012-02-17 08:15
    关注

    It looks like you are facing a serialized strings. So, instead of trying to get that number using regular expression or any other string manipulation methods, try this:

    $myVar = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
    $myNumber = $myVar['userid'];
    

    Learn about PHP serialization here:

    http://php.net/manual/en/function.serialize.php
    http://php.net/manual/en/function.unserialize.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部