提取花括号正则表达式php之间的所有值 [英] Extracting all values between curly braces regex php

查看:105
本文介绍了提取花括号正则表达式php之间的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种形式的内容

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

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

I need all the values between curly braces in an array like the one shown below:

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

我尝试了以下代码:

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

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

But I am getting in here values from first curly brace to the last found in the content. What can be done to get the array in above format? I knew that the pattern I have used is wrong. What shall be given to get desired output shown above?

推荐答案

这样做...

<?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之间的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆