我需要一个正则表达式,它可以从某个字符串中找到括号,并从中提取id的值。例如:
$string = "Hey how are you, this is [ads id=432] going to be fun!";
我需要像ads
这样的名字和像432
这样的id。
如何将这些括号替换为其他值,例如$ads = "ads here";
?
$regexp = "#'[(?<name>[^']]+) id=(?<id>[0-9]+)']#";
示例
<?php
$regexp = "#'[(?<name>[^']]+) id=(?<id>[0-9]+)']#";
$string = "Hey how are you, this is [ads id=432] going to be fun!";
preg_match($regexp, $string, $match);
print_r($match);
?>
Array
(
[0] => [ads id=432]
[name] => ads
[1] => ads
[id] => 432
[2] => 432
)
使用此正则表达式(?<='[ads's+id=)('d+?)(?='])
如果你需要一些类似广告的东西,请在正则表达式组中使用这个`(?<='[)('w+)'s+id=('d+?)(?='])
名称和值