0

How can i remove part of string from example:

@@lang_eng_begin@@test@@lang_eng_end@@ @@lang_fr_begin@@school@@lang_fr_end@@ @@lang_esp_begin@@test33@@lang_esp_end@@

I always want to pull middle of string: test, school, test33. from this string. I Read about ltrim, substr and other but I had no good ideas how to do this. Becouse each of strings can have other length for example :

'eng', 'fr'

I just want have string from middle between @@ and @@. to Maye someone can help me? I tried:

foreach ($article as $art) {

                $title = $art->titl = str_replace("@@lang_eng_begin@@", "", $art->title);
                $art->cleanTitle = str_replace("@@lang_eng_end@@", "", $title);
            }

But there

@@lang_eng_end@@

can be changed to

@@lang_ger_end@@

in next row so i ahvent idea how to fix that

0

1 Answer 1

1

If your strings are always in this format, an explode way looks easy:

$str = "@@lang_eng_begin@@test@@lang_eng_end@@ "; 
$res = explode("@@", $str)[2];
echo $res;

You may use a regex and extract the value in between the non-starting @@ and next @@:

$re = "/(?!^)@@(.*?)@@/"; 
$str = "@@lang_eng_begin@@test@@lang_eng_end@@ "; 
preg_match($re, $str, $match);
print_r($match[1]);

See the PHP demo. Here, the regex matches a @@ that is not at the string start ((?!^)@@), then captures into Group 1 any 0+ chars other than newline as few as possible ((.*?)) up to the first @@ substring.

Or, replace all @@...@@ substrings with `preg_replace:

$re = "/@@.*?@@/"; 
$str = "@@lang_eng_begin@@test@@lang_eng_end@@ "; 
echo preg_replace($re, "", $str);

See another demo. Here, we just remove all non-overlapping substrings beginning with @@, then having any 0+ chars other than a newline up to the first @@.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow thanks :) I forgot about preg_match :) Have a nice day :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.