PHP 从字符串中获取动态值
PHP Get dynamic value from string
我正在尝试从字符串中获取动态值。但是什么也没有出现。
ob_start();
var_dump($torrent->result['info']['pieces']);
$pieces = ob_get_clean();
$piecescorrected = explode($pieces, 'string(*)');
echo $piecescorrected;`
这是怎么回事?
编辑:
一些澄清。
$pieces 需要从它之后的所有其他随机字符中过滤掉。
$pieces 的输出:
string(12620) "< ÏÚÿÊܵ䬧âW—µ-‘CÄÞ½§§¼ø0LØëÍI×L —@c õL2“iÓ¹ý¼Bl'-“’4žþÊYï‡
现在需要过滤掉$pieces来修正string(12620)
但是这个值是动态的,所以我使用了 $piecescorrected = explode($pieces, 'string(*)');
注意 string(*)
中的 *
这是怎么回事:explode()
正在寻找文字字符串。它不需要通配符。
如果您有一个像 1,2,3,4
这样的字符串,您可以使用 explode(',', '1,2,3,4')
通过逗号分隔来获取这些值的数组。在这里,您可以拆分 'string'
而不是 'string(*)'
.
正如在 中所证明的那样,您实际上只需要字符串长度。
因此您不需要任何输出缓冲或 explode() 调用。只需像这样使用 strlen()
:
echo strlen($torrent->result['info']['pieces']);
输出:
12620
我正在尝试从字符串中获取动态值。但是什么也没有出现。
ob_start();
var_dump($torrent->result['info']['pieces']);
$pieces = ob_get_clean();
$piecescorrected = explode($pieces, 'string(*)');
echo $piecescorrected;`
这是怎么回事?
编辑: 一些澄清。 $pieces 需要从它之后的所有其他随机字符中过滤掉。 $pieces 的输出:
string(12620) "< ÏÚÿÊܵ䬧âW—µ-‘CÄÞ½§§¼ø0LØëÍI×L —@c õL2“iÓ¹ý¼Bl'-“’4žþÊYï‡
现在需要过滤掉$pieces来修正string(12620)
但是这个值是动态的,所以我使用了 $piecescorrected = explode($pieces, 'string(*)');
注意 string(*)
这是怎么回事:explode()
正在寻找文字字符串。它不需要通配符。
如果您有一个像 1,2,3,4
这样的字符串,您可以使用 explode(',', '1,2,3,4')
通过逗号分隔来获取这些值的数组。在这里,您可以拆分 'string'
而不是 'string(*)'
.
正如在
因此您不需要任何输出缓冲或 explode() 调用。只需像这样使用 strlen()
:
echo strlen($torrent->result['info']['pieces']);
输出:
12620