PHP正则表达式:括号内的字符串 [英] PHP Regular Expression: string from inside brackets

查看:83
本文介绍了PHP正则表达式:括号内的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:

$a = "Name[value]";

如何从字符串中将字符串的名称"和值"部分转换为变量?我在正则表达式方面不怎么好.谢谢!

How do I get the 'Name' and 'value' portions of string into variables from this string? I'm not that great at regular expressions. Thanks!

推荐答案

所以这应该为您解决问题:

so this should do the trick for you:

(.*)\[(.*)\]

这是PHP语法:

<?php
$subject = "Name[value]";
$pattern = '/(.*)\[(.*)\]/';
preg_match($pattern, $subject, $matches);
print_r($matches);
?>

输出:

Array
(
    [0] => Name[value]
    [1] => Name
    [2] => value
)

享受.

这篇关于PHP正则表达式:括号内的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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