使用 PHP 从较大字符串中删除文本内部的文本

发布于 2024-12-12 12:23:59 字数 226 浏览 3 评论 0原文

我想做的是,如果存在,删除“短代码”中出现的文本,例如:这里有一些内容[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

最初的梦 2024-12-19 12:23:59

试试这个:

$var = "Here's some content [shortcode]I want this text removed[/shortcode] Some more content";
$startTag = "[shortcode]";
$endTag = "[/shortcode]";
$pos1 = strpos($var, $startTag) + strlen($startTag);
$pos2 = strpos($var, $endTag);
$result = substr_replace($var, '', $pos1, $pos2-$pos1);

Try this:

$var = "Here's some content [shortcode]I want this text removed[/shortcode] Some more content";
$startTag = "[shortcode]";
$endTag = "[/shortcode]";
$pos1 = strpos($var, $startTag) + strlen($startTag);
$pos2 = strpos($var, $endTag);
$result = substr_replace($var, '', $pos1, $pos2-$pos1);
知足的幸福 2024-12-19 12:23:59

使用 preg_replace() 很容易做到。出于您的目的,请使用 /\[shortcode\].*\[\/shortcode\]/ 作为模式。

$replace = "[shortcode][/shortcode]"; 
$filteredText = preg_replace("/\[shortcode\].*\[\/shortcode\]/", $replace, $yourContent);

请参阅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.

$replace = "[shortcode][/shortcode]"; 
$filteredText = preg_replace("/\[shortcode\].*\[\/shortcode\]/", $replace, $yourContent);

See http://php.net/manual/en/function.preg-replace.php for more details.

谎言 2024-12-19 12:23:59

可以使用 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()

新雨望断虹 2024-12-19 12:23:59

如果您不想使用正则表达式:

如果字符串中确实有 [shortcode] 标记,那么这真的没问题:只需使用 substr 的嵌套使用:

substr($string,0,strpos($string,'[substring]')+11)+substr($string,strpos($string,'[/substring]'),strlen($string))

其中第一个 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:

substr($string,0,strpos($string,'[substring]')+11)+substr($string,strpos($string,'[/substring]'),strlen($string))

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

你怎么敢 2024-12-19 12:23:59

在 php 中使用正则表达式来摆脱它。

preg_replace (shortcode, urText, '', 1)

use regex in php to get rid of it.

preg_replace (shortcode, urText, '', 1)
橘香 2024-12-19 12:23:59
$string = "[shortcode]I want this text removed[/shortcode]"; 
$regex = "#\[shortcode\].*\[\/shortcode\]#i"; 
$replace = "[shortcode][/shortcode]"; 
$newString = preg_replace ($regex, $replace, $string, -1 );  
$string = "[shortcode]I want this text removed[/shortcode]"; 
$regex = "#\[shortcode\].*\[\/shortcode\]#i"; 
$replace = "[shortcode][/shortcode]"; 
$newString = preg_replace ($regex, $replace, $string, -1 );  
乱了心跳 2024-12-19 12:23:59
$content = "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";
print preg_replace('@(\[shortcode\])(.*?)(\[/shortcode\])@', "$1$3", $content);

产量:

这是一些内容 [shortcode][/shortcode] 一些更多的内容要更改为 这是一些内容 [shortcode][/shortcode] 一些更多的内容

$content = "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";
print preg_replace('@(\[shortcode\])(.*?)(\[/shortcode\])@', "$1$3", $content);

Yields:

Here's some content [shortcode][/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文