PHP:如何删除“之间的字符串”;[“和”和“];
我需要删除[…]中的字符串,包括“[]”本身。我试图从这个网站上寻找解决方案。我有一个线索,我应该尝试一些与preg_替换,但它似乎太专家对我来说 例如:PHP:如何删除“之间的字符串”;[“和”和“];,php,preg-replace,Php,Preg Replace,我需要删除[…]中的字符串,包括“[]”本身。我试图从这个网站上寻找解决方案。我有一个线索,我应该尝试一些与preg_替换,但它似乎太专家对我来说 例如: [gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] Description The thirty-two storey resort condominium in Phuket, located Just 150m from Patong beach where choices
[gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] Description The thirty-two storey resort condominium in Phuket, located Just 150m from Patong beach where choices of activities, water sp
我需要从示例文本中删除[gallery id=“92,93,94,95,96,97,98,99100”orderby=“rand”]。它总是以[gallery ids=“开头
请建议。试试这个:
$string = '[gallery ids="92,93,94,95,96,97,98,99,100" orderby="rand"] Description The thirty-two storey resort condominium in Phuket, located Just 150m from Patong beach where choices of activities, water sp ';
$string = preg_replace('/\[gallery ids=[^\]]+\]/', '', $string);
细分:
\[gallery ID=
查找以[gallery ID=
开头的子字符串
[^\]+\]
匹配一个或多个非]
字符,直到您到达]
然后将整个匹配部分替换为
''
无,您就有了新字符串。您的问题标题令人困惑:您不希望删除“[”和“]”之间的字符串,您希望删除以“[”开头并以“]”结尾的字符串(包括括号).我只是意外地否决了一个有效的答案(现在被撤销),因为我误解了…@RaphaelSchweikert在[…]中,包括“[]”本身,这就是我要说的。谢谢。问题解决了。