PHP Preg 匹配 - 获取包装值

标签 php regex preg-match

我想使用预匹配提取所有换行的文本值

所以

background: url("images/gone.png");
color: #333;
...

background: url("images/good.png");
font-weight: bold;

从上面的字符串, 我要抢

images/gone.png
images/good.png

正确的命令行是什么?

最佳答案

在 php 中,你应该这样做:

$str = <<<CSS
    background: url("images/gone.png");
    color: #333;

    background: url("images/good.png");
    font-weight: bold;
CSS;

preg_match_all('/url\("(.*?)"\)/', $str, $matches);
var_dump($matches);

然后,你会看到这样的输出:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(22) "url("images/gone.png")"
    [1]=>
    string(22) "url("images/good.png")"
  }
  [1]=>
  array(2) {
    [0]=>
    string(15) "images/gone.png"
    [1]=>
    string(15) "images/good.png"
  }
}

因此,带有 url 的列表将位于 $matches[1] :)

关于PHP Preg 匹配 - 获取包装值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34431926/

相关文章:

PHP 在不知道有多少列的情况下显示所有 MySQL 列

php - 如何在 PHP 中将 "decimal string"转换为没有句点的整数

java - 从文件读取时省略所有空行

python - 正则表达式在 Python 中拆分单词

php - preg_match :print: class matches tab character

php - 奇怪的 PHP 正则表达式 Preg_Match Bug?

php - 如何使用 `preg_match()` 匹配用户名

php - 如何从mysql表中选择多个枚举值?

php - 创建 PHP DOM xml 文件并创建保存文件链接/提示,而无需在 header 已发送时将文件写入服务器

c# - 搜索文件内容以查找与正则表达式的匹配项