亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何編寫在 $_POST['some_string'] 之后刪除所有內容的正則表達式

如何編寫在 $_POST['some_string'] 之后刪除所有內容的正則表達式

PHP
幕布斯6054654 2021-08-27 10:34:04
我有以下字符串: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"]'
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

使用正則表達式 ^\$_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"]


查看完整回答
反對 回復 2021-08-27
  • 1 回答
  • 0 關注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號