我有一個字符串。我需要刪除render_dynamic_content(,留下文本,然后)從字符串中刪除 next 。模式內(nèi)的文本可以不同。例子:some ( text {{ render_dynamic_content(dynamic_html.POS_A_IMG) }} another ) text {{ render_dynamic_content(dynamic_html.POS_B_IMG) }}預(yù)期結(jié)果應(yīng)該是:some ( text {{ dynamic_html.POS_A_IMG }} another ) text {{ dynamic_html.POS_B_IMG }}我想:$string = 'some ( text {{ render_dynamic_content(dynamic_html.POS_A_IMG) }} another ) text {{ render_dynamic_content(dynamic_html.POS_B_IMG) }}';
$content = preg_replace('/render_dynamic_content([\s\S]+?)/', '', $string);結(jié)果是:some ( text {{ dynamic_html.POS_A_IMG) }} another ) text {{ dynamic_html.POS_B_IMG) }}我需要)在dynamic_html.POS_A_IMG和之后刪除dynamic_html.POS_B_IMG
1 回答

慕哥6287543
TA貢獻1831條經(jīng)驗 獲得超10個贊
您已經(jīng)接近了,但是您需要在您的模式中包含(并轉(zhuǎn)義)括號,然后將捕獲的部分(您要保留的部分)放在替換中而不是空字符串中。
$string = 'some ( text {{ render_dynamic_content(dynamic_html.POS_A_IMG) }} another ) text {{ render_dynamic_content(dynamic_html.POS_B_IMG) }}';
$content = preg_replace('/render_dynamic_content\(([\s\S]+?)\)/', '\1', $string);
echo $content;
注意:某些其他模式可能更有效,也可能無效。出于這個答案的目的,我不打算嘗試優(yōu)化它。
- 1 回答
- 0 關(guān)注
- 180 瀏覽
添加回答
舉報
0/150
提交
取消