我正在寻找一个正则表达式,它可以用"->"替换所有"[",但前提是后面没有"]"。
并同时将所有"]"替换为零,但仅当它们不在"["旁边时
所以换句话说,"test[hi][]"将变成"test->hi[]"
谢谢;)
我真的不知道该怎么做;)
我已经假设括号之间的内容遵循PHP变量命名约定(即字母、数字、下划线),并且您的代码是有效的(例如没有$test['five]
)。
echo preg_replace('/'[[''"]?('w+)[''"]?']/', '->'1', $input);
这应该处理:
test[one]
test['two']
test["three"]
但不是:
test[$four]
不需要正则表达式!
strtr($str, array('[]'=>'[]','['=>'->',']'=>''))
$ cat 1.php
<?php
echo strtr('[hi][]', array('[]'=>'[]','['=>'->',']'=>''));
$ php 1.php
->hi[]
将此正则表达式'[('w+)']
替换为->
+匹配组1
应该这样做。它使用
'[ # match a [
( # match group
[^']]+ # match everything but a ] one or more times
) # close match group
'] # match ]
匹配括号之间的任何内容
$replaced = preg_replace("/'[([^']]+)']/", "->$1", $string);