猿问

如何编写在 $_POST['some_string'] 之后删除所有内容的正则表达式

我有以下字符串:

string = '$_POST["a_string_of_unspecified_length"][4]input&set1';

如何编写一个仅返回$_POST["a_string_of_unspecified-length"]并丢弃第一组括号后的所有内容的正则表达式

$string_afer_regex = '$_POST["a_string_of_unspecified_length"]'


幕布斯6054654
浏览 135回答 1
1回答

Helenr

使用正则表达式 ^\$_POST\[[^]]+\]$str = '$_POST["a_string_of_unspecified_length"][4]input&set1';preg_match('~^\$_POST\[[^]]+\]~', $str, $matches);print_R($matches);// $_POST["a_string_of_unspecified_length"]^     - beginning of string\$    - escaped dollar sign_POST - normal characters, just part of string\[    - escaped '['[^]]+ - everything till ']', + means 'more than 1 character'\]    - escaped '['the rest doesn't care us, there can be whatever如果需要,没有正则表达式$str = '$_POST["a_string_of_unspecified_length"][4]input&set1';echo substr($str, 0, strpos($str, ']') + 1);// $_POST["a_string_of_unspecified_length"]
随时随地看视频慕课网APP
我要回答