Reputation:
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
Upvotes: 0
Views: 2716
Reputation: 5974
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;
Upvotes: 0
Reputation: 818
$string = '//[langStart-en] This is a test //[langEnd-en]';
$string = preg_replace(
'/\/\/\[langStart-en\][\s\S]+?\/\/\[langEnd-en\]/',
'//[langStart-en]//[langEnd-en]',
$string
);
Upvotes: 0
Reputation: 45565
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]');
Upvotes: 1
Reputation: 4499
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.
Upvotes: 0
Reputation: 27944
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;
Upvotes: 1