替换给定起点和终点的字符串内的字符串
当给出开始和结束时,您能帮我在字符串中插入字符串吗? 删除 //[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 errorWarning: preg_replace() [function.preg-replace]: Unknown modifier '/' : eval()'d code on line 4"
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您可以连接给定的字符串,为什么要删除给定字符串之间的字符串呢?他们会给你同样的结果。
Why remove the string between the given strings if you can concatenate the strings you are given. They will give you the same result.
给你:
//[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.
嗯,我没有讽刺的意思。
或者
Yah, I don't mean to be sarcastic.
Or
不确定您使用什么语言来执行此操作。
但大多数语言都有一个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.