PHP Remove string between [:en] [:]

202 views Asked by At

How can I remove string from between square brackets and the brackets themselves with regex?

Let me clarify, is not that case: [some text] but this one:

Lorem ipsum[:en]Some text[:]

After "str_replace" the variable should contain just:

Lorem Ipsum

Someone can help me? I'm going crazy :)

3

There are 3 answers

8
Vaibhavi S. On

Try this to remove between [] from string.

 $str = "Lorem ipsum[:en]Some text[:]";
   $new_str = preg_replace('`\[[^\]]*\]`',' ',$str);

   echo $new_str;

OUTPUT

Lorem ipsum Some text

DEMO

0
Toto On

Use:

\[.+?].+?\[.+?]

Explanation:

\[      # openning square bracket
.+?     # 1 or more any character, not greedy
]       # closing square bracket
.+?     # 1 or more any character, not greedy
\[      # openning square bracket
.+?     # 1 or more any character, not greedy
]       # closing square bracket

Code:

$text = 'Lorem ipsum[:en]Some text[:] Lorem ipsum[:en]Some text[:] Lorem ipsum';
echo preg_replace('/\[.+?].+?\[.+?]/', '', $text);

Output:

Lorem ipsum Lorem ipsum Lorem ipsum
0
wawa On

You can use ~\[:\w{2}\].*?\[:\]~ as your regex.

Code:

$str = "Lorem ipsum [:en]Some text[:] dolor [:en]sit amet[:]";
$new_str = trim(preg_replace(['~\[:\w{2}\].*?\[:\]~', '~\s\s+~'],[' ', ' '], $str));
echo $new_str;

// besides running the regex, this also takes care of multiple whitespaces and whitespaces at the begin and end.

It will transform Lorem ipsum [:en]Some text[:] dolor [:en]sit amet[:] to Lorem ipsum dolor It will only match whats between [:XX] and [:] (where XX are two alphanumeric characters). This means, Lorem [foobar] ipsum [baz] will stay as it is and not be changed (as I guess, this is what you're looking for.

Examples:

Input: "Lorem ipsum [:en]Some text[:] dolor [:en]sit amet[:]"
Output: "Lorem ipsum dolor"
Input: "Lorem ipsum[:en]Some text[:] dolor[:en]sit amet[:]"
Output: "Lorem ipsum dolor"
Input: "Lorem [foobar] ipsum [baz]"
Output: "Lorem [foobar] ipsum [baz]"

See it in action!

Explanation:
\[:\w{2}\].*?\[:\]

\[    # matches the character [ literally (case sensitive)
:     # matches the character : literally (case sensitive)
\w{2} # matches any word character (equal to [a-zA-Z0-9_])
  {2} # Quantifier — Matches exactly 2 times
\]    # matches the character ] literally (case sensitive)
.*?   # matches any character (except for line terminators)
 *?   # Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\[    # matches the character [ literally (case sensitive)
:     # matches the character : literally (case sensitive)
\]    # matches the character ] literally (case sensitive)