Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中从字符串中提取单个值_Php_Extraction - Fatal编程技术网

在PHP中从字符串中提取单个值

在PHP中从字符串中提取单个值,php,extraction,Php,Extraction,我有这样一个字符串: $string = 'rgb(178, 114, 113)'; 我想提取这些数据的各个值 $red = 178; $green = 114; $blue = 113; 如果您的字符串总是以rgb(开头,以结尾)开头,那么您可以使用 $rgb = substr($string, 4, -1); //4 is the starting index, -1 means second to last character 然后: 此时,$VAL[0]为红色,$VAL[1]为绿色

我有这样一个字符串:

$string = 'rgb(178, 114, 113)';
我想提取这些数据的各个值

$red = 178;
$green = 114;
$blue = 113;

如果您的字符串总是以
rgb(
开头,以
结尾)
开头,那么您可以使用

$rgb = substr($string, 4, -1); //4 is the starting index, -1 means second to last character
然后:


此时,
$VAL[0]
为红色,
$VAL[1]
为绿色,
$VAL[2]
为蓝色。

如果字符串始终以
rgb(
开头,以
结尾)
开头,则可以使用

$rgb = substr($string, 4, -1); //4 is the starting index, -1 means second to last character
然后:

此时,
$VAL[0]
为红色,
$VAL[1]
为绿色,
$VAL[2]
为蓝色。

您可以使用

输出:

Array
(
    [0] => Array
        (
            [0] => 178
            [1] => 114
            [2] => 113
        )
)
希望这有帮助。

您可以使用

输出:

Array
(
    [0] => Array
        (
            [0] => 178
            [1] => 114
            [2] => 113
        )
)

希望这有帮助。

使用preg\u match\u all和list,您可以获得所需的变量:

$string = "rgb(178, 114, 113)";
$matches = array();
preg_match_all('/[0-9]+/', $string, $matches);
list($red,$green,$blue) = $matches[0];

请注意,这并不验证原始字符串实际上是否有三个整数值。

使用preg\u match\u all和list,您可以获得所需的变量:

$string = "rgb(178, 114, 113)";
$matches = array();
preg_match_all('/[0-9]+/', $string, $matches);
list($red,$green,$blue) = $matches[0];

请注意,这不会验证原始字符串实际上是否有三个整数值。

向我们展示您的尝试。向我们展示您的尝试。谢谢,这很有效!谢谢大家!谢谢,成功了!谢谢大家!