替换给定起点和终点的字符串内的字符串

发布于 2024-11-17 06:04:50 字数 631 浏览 4 评论 0原文

当给出开始和结束时,您能帮我在字符串中插入字符串吗? 删除 //[langStart-en]//[langEnd-en] 之间的所有内容

//[langStart-en]  This is a test //[langEnd-en]

实际上我想在下面的示例中使用 preg_replace。我使用了以下代码

  $string = '//[langStart-en] This is a test //[langEnd-en]';
    $pattern = '/\/\/\[langStart-en\][^n]*\/\/\[langEnd-en\]/';
    $replacement = '//[langStart-en]//[langEnd-en]';
    $my_string = preg_replace($pattern, $replacement, $string);

    echo $my_string;

它显示以下错误 警告:preg_replace() [function.preg-replace]:未知修饰符“/”:第 4 行上的 eval() 代码 请帮忙

Could you please help me to a string inside a string when start and end are given. Actually I want to delete all the contents between //[langStart-en] and //[langEnd-en] in the following example

//[langStart-en]  This is a test //[langEnd-en]

using preg_replace. I used the following code

  $string = '//[langStart-en] This is a test //[langEnd-en]';
    $pattern = '/\/\/\[langStart-en\][^n]*\/\/\[langEnd-en\]/';
    $replacement = '//[langStart-en]//[langEnd-en]';
    $my_string = preg_replace($pattern, $replacement, $string);

    echo $my_string;

It is showing the following error
Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' : eval()'d code on line 4"
Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

心安伴我暖 2024-11-24 06:04:50

如果您可以连接给定的字符串,为什么要删除给定字符串之间的字符串呢?他们会给你同样的结果。

string c = a + b;

Why remove the string between the given strings if you can concatenate the strings you are given. They will give you the same result.

string c = a + b;
云醉月微眠 2024-11-24 06:04:50

给你:

//[langStart-en]//[langEnd-en]

对于那些缺乏幽默感的人 - 真丢脸。但这里有一个答案。

var str = '//[langStart-en] This is a test //[langEnd-en]';
str.replace(/\/\/\[langStart-en\].+\/\/\[langEnd-en\]/g, '//[langStart-en]//[langEnd-en]');

Here you go:

//[langStart-en]//[langEnd-en]

For those of you with less sense of humour - shame on you. But here's an answer.

var str = '//[langStart-en] This is a test //[langEnd-en]';
str.replace(/\/\/\[langStart-en\].+\/\/\[langEnd-en\]/g, '//[langStart-en]//[langEnd-en]');
岁月流歌 2024-11-24 06:04:50

嗯,我没有讽刺的意思。

$my_string = '//[langStart-en]//[langEnd-en]';

或者

$string = '//[langStart-en] This is a test //[langEnd-en]';
$pattern = '/\/\/\[langStart-en\][^n]*\/\/\[langEnd-en\]/';
$replacement = '//[langStart-en]//[langEnd-en]';
$my_string = preg_replace($pattern, $replacement, $string);

echo $my_string;

Yah, I don't mean to be sarcastic.

$my_string = '//[langStart-en]//[langEnd-en]';

Or

$string = '//[langStart-en] This is a test //[langEnd-en]';
$pattern = '/\/\/\[langStart-en\][^n]*\/\/\[langEnd-en\]/';
$replacement = '//[langStart-en]//[langEnd-en]';
$my_string = preg_replace($pattern, $replacement, $string);

echo $my_string;
給妳壹絲溫柔 2024-11-24 06:04:50

不确定您使用什么语言来执行此操作。

但大多数语言都有一个indexof函数。

var mystring - “cccctestooabcccc”;
var i = mystring.indexof("测试");
var x = mystring.indexof("abc");

对于这些索引,您可以使用类似 substring(startindex, endindex); 的函数。

不过,您必须添加或减去字符串的长度(test 或 abc)
因为索引是第一个字符的位置。
所以
我 = 4 且 x = 11
你会想要在 ((i + "test".length), x) 之间拉出子字符串
希望拉出子字符串“ooo”

这很粗糙,但应该给你一个总体思路。

Not sure what language your using to do this.

But most languages have an indexof function.

var mystring - "cccctestoooabcccc";
var i = mystring.indexof("test");
var x = mystring.indexof("abc");

With those indexs you can use a function like substring(startindex, endindex);

Although, you will have to add or subtract the length of your string (test or abc)
Because the the index is of the first character location.
So
i = 4 and x = 11
you'd would want to pull the substring between ((i + "test".length), x)
Hopefully pull the substring "ooo"

This is rough, but should give you the general idea.

策马西风 2024-11-24 06:04:50
$string = '//[langStart-en] This is a test //[langEnd-en]';
$string = preg_replace(
      '/\/\/\[langStart-en\][\s\S]+?\/\/\[langEnd-en\]/',
      '//[langStart-en]//[langEnd-en]',
      $string
 );
$string = '//[langStart-en] This is a test //[langEnd-en]';
$string = preg_replace(
      '/\/\/\[langStart-en\][\s\S]+?\/\/\[langEnd-en\]/',
      '//[langStart-en]//[langEnd-en]',
      $string
 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文