0

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

0

5 Answers 5

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I have a huge string but have to remove just the data between the above mentioned tags.
aren't strings immutable? That means that if you do operations on them they will be copied anyway. Is there data before the tag and after the tag? Then copying the string before and after will still give you the best performance.
1

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]');

Comments

0

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;

11 Comments

@Steve Smith realize that I'm going off of your own input. You gave me spaces, uppercase/lowercase letters, and that's what I searched to replace.
THere can be many similar tags like //[langStart-en] in the string. Also there can be any number of charactors/sentances between the tags
Getting the following error.. Delimiter must not be alphanumeric or backslash
It is showing the following error Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' : eval()'d code on line 4
@Steve Smith show me your updated code snippet. Put it here, dpaste.com
|
0

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.

Comments

0
$string = '//[langStart-en] This is a test //[langEnd-en]';
$string = preg_replace(
      '/\/\/\[langStart-en\][\s\S]+?\/\/\[langEnd-en\]/',
      '//[langStart-en]//[langEnd-en]',
      $string
 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.