php - 提取大括号正则表达式php之间的所有值

标签 php regex preg-match

我有这种形式的内容

$content ="<p>This is a sample text where {123456} and {7894560} ['These are samples']{145789}</p>";

我需要数组中大括号之间的所有值,如下所示:

array("0"=>"123456","1"=>"7894560","2"=>"145789")

我试过这段代码:

<?php
preg_match_all("/\{.*}\/s", $content, $matches);
?>

但我在这里获取从内容中第一个大括号到最后一个大括号的值。如何获得上述格式的数组?我知道我使用的模式是错误的。应该给出什么来获得上面显示的期望输出?

最佳答案

像这样...

<?php
$content ="<p>This is a sample text where {123456} and {7894560} ['These are samples']{145789}</p>";
preg_match_all('/{(.*?)}/', $content, $matches);
print_r(array_map('intval',$matches[1]));

输出:

Array
(
    [0] => 123456
    [1] => 7894560
    [2] => 145789
)

关于php - 提取大括号正则表达式php之间的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20838624/

相关文章:

php - 正则表达式检查另一个字符串中是否存在一个字符串

java - 关于在没有专用服务器的情况下托管 Java 应用程序

php - 如何使用 PHP 的实时反馈运行 shell 脚本?

javascript - 有人可以向我解释一下这个正则表达式吗?

php - 正则表达式确保字符串仅包含管道、数字和/或逗号,

php - 使用预匹配过滤掉 iframe

正则表达式 - 匹配除特定字符串以外的任何内容

php - 如何在 PHP 中改进以下代码

php - 执行原始插入时如何在 Laravel 5.1 中强制更新时间戳...在重复键更新时?

regex - 使用正则表达式匹配所有大于 36 的数字?