dsirr48088 2013-03-28 18:00
浏览 235
已采纳

如何从php中的字符串中获取冒号分隔值

I have a string

$style = "font-color:#000;font-weight:bold;background-color:#fff";

I need only

font-color
font-weight
background-color

I have tried

preg_match_all('/(?<names>[a-z\-]+:)/', $style, $matches);

var_dump($matches);

it gives me following output

array
  0 => 
    array
      0 => string 'font-color:' (length=11)
      1 => string 'font-weight:' (length=12)
      2 => string 'background-color:' (length=17)
  'names' => 
    array
      0 => string 'font-color:' (length=11)
      1 => string 'font-weight:' (length=12)
      2 => string 'background-color:' (length=17)
  1 => 
    array
      0 => string 'font-color:' (length=11)
      1 => string 'font-weight:' (length=12)
      2 => string 'background-color:' (length=17)

There are three problems with this output 1. It is two or three dimensional array, I need one dimensional array. 2. It is repeating the information 3. It is appending ":" at the end of each element.

I need a single array like this

array
0 => 'font-color'
1 => 'font-weight'
2 => 'background-color'
  • 写回答

1条回答 默认 最新

  • dqz86173 2013-03-28 18:06
    关注

    Take out the colon:

    $style = "font-color:#000;font-weight:bold;background-color:#fff";
    preg_match_all('/(?<names>[a-z\-]+):/', $style, $matches);
    
    var_dump($matches['names']);
    

    Then use $matches['names'], since you named it, so you dont have redundant informations

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类