dongzhi5587 2013-01-30 09:49
浏览 13
已采纳

使用PHP在CSS中搜索内容

I have that in a variable :

.fontSize10{
font-size:xx-small;
}
.fontSize11{
font-size:x-small;
}
.fontSize12{
font-size:small;
}
.fontSize13{
font-size:medium;
}
.fontSize14{
font-size:large;
}
.fontSize16{
font-size:x-large;
}
.fontSize18{
font-size:xx-large;
}

In this variable, i can have a lots of other css instructions.

I need to retrieve, for each instructions that begin by .fontSize, his name (for example .fontSize14) and his size (large).

I need to keep name and size together (in an array for example $tab[0][0] = "fontSize14" and $tab[0][1] = "large")

Do you have an idea ? With regex maybe ? Thanks :)

  • 写回答

3条回答 默认 最新

  • donglao4370 2013-01-30 09:55
    关注

    If it strictly keeps to this format, you could do it with a regular expression like this:

    =\.(fontSize\d+)\s*\{\s*font-size:([^;]+);\s*\}=is
    

    However, I would advise you to write a proper parser so you can deal with wrong or different formats. Meaning that you iterate through the string line by line and store in a variable the name of the current definition block. If you encounter a font-size: you can just add the found value to an array using the name of the current block. When you encounter a closing bracket, you just set the variable back to null. It will be much more robust than the regular expression, because it works for CSS definitions with more attributes just as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看