php - 使用 preg_replace 提取属性

标签 php css string function preg-replace

我想提取所有属性(并能够在以后操作它们)。

iow... {} 之间的所有内容都将被提取。

<?php

$css = <<<EOF

body {
z-index : 9;
padding: 0;
margin: 0;
line-height: 10px;
}

p {
z-index: 9;
font-size: 10px;
}

h1,h2,h3,h4,h5,h6 {
z-index: 2;
padding: 0;
margin: 0;
font-size: 100%;
border: 0 none;
}

EOF;

echo preg_replace( '~{(.*)}~s', '$1<br/>' , $css );

?>

我希望通过换行符提取所有属性(并能够在以后操作它们):

z-index : 9;
padding: 0;
margin: 0;
line-height: 10px;

z-index: 9;
font-size: 10px;

z-index: 2;
padding: 0;
margin: 0;
font-size: 100%;
border: 0 none;

最佳答案

对于这个特定的输入字符串,试试这个-

echo preg_replace( '~.*?{(.*?)}~s', '$1<br/>' , $css );

正则表达式的演示和解释Here .
输出-

z-index : 9;
padding: 0;
margin: 0;
line-height: 10px;
<br/>
z-index: 9;
font-size: 10px;
<br/>
z-index: 2;
padding: 0;
margin: 0;
font-size: 100%;
border: 0 none;
<br/>

关于php - 使用 preg_replace 提取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22568860/

相关文章:

php - Laravel 8 在 api 请求时返回群错误

php - MySQL 时间戳列的时间戳不匹配

jquery - Highcharts 中的堆叠区域使我的系列淡入和淡出

c - c字符串构造

php - GEOIP 和获取计算机 IP 地址位置?

php - 正则表达式 - 匹配 [[[ 和 ]]] 之间任何值的倍数

css - 从 % 切换到 px 时如何覆盖 !important

css - 为什么这个简单的 CSS 定义不起作用?

android - 从阿拉伯语文本文件中搜索阿拉伯语单词

string - squeak(smalltalk)如何使用方法 `findSubstring: in: startingAt: matchTable:`?