2

我需要删除 [...] 内的字符串,包括“[]”本身。我尝试从该站点搜索解决方案。我有一个线索,我应该用 preg_replace 尝试一些东西,但它对我来说似乎太专业了。

例如 :

[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 ids="92,93,94,95,96,97,98,99,100" orderby="rand"] 。它总是以[gallery ids="开头。

请建议。

4

1 回答 1

7

试试这个:

$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 ids=寻找以[gallery ids=

[^\]]+\]匹配 1 个或多个字符,]直到您到达]

然后将用''任何内容替换整个匹配的部分,你就有了新的字符串。

于 2013-03-03T16:02:25.600 回答