2015-08-27 94 views
0

个人价值观我有一个这样的字符串:从字符串中提取的PHP

$string = 'rgb(178, 114, 113)'; 

,我想提取的那

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

告诉我们你试过了什么。 – axiac

回答

4

您可以使用regular expression

preg_match_all('(\d+)', $string, $matches); 
print_r($matches); 

输出:

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

希望这有助于。

+0

谢谢,这工作!谢谢大家! – redviper2100

1

个人价值。如果你的字符串将始终rgb(启动并以)结尾,那么你可以truncate the string178, 114, 113

然后convert the list to an array

$vals = explode(', ', $rgb); 
//or you could use just ',' and trim later if your string might be in the format `123,123, 123` (i.e. unknown where spaces would be) 

在这一点上,$vals[0]是红色的,$vals[1]为绿色,$vals[2]是蓝色的。

0

使用preg_match_all和列表,你可以得到你想要的变量:

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

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