PHP从字符串中提取文本

发布于 2025-01-06 10:59:43 字数 110 浏览 1 评论 0原文

我有以下字符串: a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}

我需要做的是从中提取数字 48它,在这种情况下。该数字可以有任意位数。我该怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

王权女流氓 2025-01-13 10:59:44

看起来您面对的是序列化的字符串。因此,不要尝试使用正则表达式或任何其他字符串操作方法来获取该数字,而是尝试以下操作:

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

在此处了解 PHP 序列化:

http://php.net/manual/en/function.serialize.php< /a>
http://php.net/manual/en/function.unserialize.php

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

蒲公英的约定 2025-01-13 10:59:44

您到底想要实现什么目标?该字符串看起来像一个 serialize()d 字符串,因此最好的选择是 unserialize()

What exactly are you trying to achieve? That string looks like a serialize()d one, so your best bet would be to unserialize() it

沫雨熙 2025-01-13 10:59:44

它看起来像序列化的字符串。

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

It looks like serialized string.

$data = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
print_r($data['userid']);
一花一树开 2025-01-13 10:59:44

看起来这是一个序列化的关联数组。您只需要使用 unserialize() 将其从字符串转回数组。

<?php
  $arr = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
  echo $arr['userid'];
?>

Looks like that's a serialized associative array. You just need to use unserialize() to turn it back from a string into an array.

<?php
  $arr = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
  echo $arr['userid'];
?>
來不及說愛妳 2025-01-13 10:59:44

我看到的字符串是 PHP 中的序列化数组

要反序列化数组,请执行此操作,

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

我有反序列化数组,然后按名称访问数组参数

The string I see is serialized array in PHP

To unserialize array do this

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

I have unserialized array then access array param by name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文