使用 PHP 从较大字符串中删除文本内部的文本
我想做的是,如果存在,删除“短代码”中出现的文本,例如:这里有一些内容[shortcode]我希望删除此文本[/shortcode]更多内容
更改为 这里有一些内容 [shortcode][/shortcode] 还有一些内容
。
这似乎是一件非常简单的事情,但我无法弄清楚.. =/
短代码在整个字符串中只会出现一次。
预先感谢您的帮助。
What I'm trying to do is, if it exists, remove an occurrence of text inside a 'shortcode', eg: Here's some content [shortcode]I want this text removed[/shortcode] Some more content
to be changed to Here's some content [shortcode][/shortcode] Some more content
.
It seems like a pretty simple thing to do but I can't figure it out.. =/
The shortcode will only show up once in the entire string.
Thanks in advance for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
Try this:
使用 preg_replace() 很容易做到。出于您的目的,请使用
/\[shortcode\].*\[\/shortcode\]/
作为模式。请参阅http://php.net/manual/en/function.preg-replace。 php 了解更多详细信息。
It's very easy to do with preg_replace(). For your purpose, use
/\[shortcode\].*\[\/shortcode\]/
as pattern.See http://php.net/manual/en/function.preg-replace.php for more details.
可以使用 strpos() 查找字符串中 [substring] 和 [/substring] 的位置,并通过 substr_replace() 将文本替换为空格
One can use strpos() to find the position of [substring] and [/substring] in your string and replace the text with a whitespace via substr_replace()
如果您不想使用正则表达式:
如果字符串中确实有
[shortcode]
标记,那么这真的没问题:只需使用 substr 的嵌套使用:其中第一个 substr将字符串剪切到要剪切的字符串的开头,第二个添加字符串的剩余内容。
请参阅此处:
http://www.php.net/manual/en/function。 substr.php
http://www.php.net/manual/ en/function.strpos.php
if you do not want to bother with regular expessions:
if you do have the
[shortcode]
tag inside the string, than it is really no problem: just use a nested use of substr:where the first substr cuts the string to the start of the string to cut and the second adds the remaining stuff of the string.
see here:
http://www.php.net/manual/en/function.substr.php
http://www.php.net/manual/en/function.strpos.php
在 php 中使用正则表达式来摆脱它。
use regex in php to get rid of it.
产量:
这是一些内容 [shortcode][/shortcode] 一些更多的内容要更改为 这是一些内容 [shortcode][/shortcode] 一些更多的内容
Yields:
Here's some content [shortcode][/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content