-2

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 :)

theduck
  • 2,589
  • 13
  • 17
  • 23
Marco
  • 1

3 Answers3

0

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

Vaibhavi S.
  • 1,083
  • 7
  • 20
  • What about multiple tags like in `Lorem ipsum[:en]Some text[:] Lorem ipsum[:en]Some text[:] Lorem ipsum` – Toto Oct 03 '19 at 12:00
  • 1
    No, it doesn't. It keeps only the first part. – Toto Oct 03 '19 at 12:06
  • I have also tried please check demo here https://paiza.io/projects/BM0mPleRSOHt0gaHRN6kgg?language=php – Vaibhavi S. Oct 03 '19 at 12:11
  • There are multiple issues with this. First of all, you probably have missed a `.` before the `*`, so I think it should be: `'~\[[^\]].*\]~'` now as @Toto pointed out, it's greedy therefore it will change "Foo [:en]Bar[:] Baz [:en] Foobar[:]" to "Foo" instead of "Foo Baz". Plus it would also change "Foo [bar] baz [bar]" to "Foo", while it should only work with `[:XX]...[:]` – wawa Oct 03 '19 at 12:18
  • if i add `.` before the `*` it is returning only first value. you can check in demo link @wawa – Vaibhavi S. Oct 03 '19 at 12:22
  • Ah, now I understand, what you're doing. I think you misunderstood his question. Your regex would turn "Lorem ipsum[:en]Some text[:]" into "Lorem ipsum Some text ", while he's looking for "Lorem ipsum" as result, He wants to remove everything which is between `[:en]` and `[:]` – wawa Oct 03 '19 at 12:36
  • oh.. okay @wawa – Vaibhavi S. Oct 03 '19 at 12:38
  • Code posted in answer doesn't work, but the code in Demo link works fine! You missed a `.` before `*` – Marco Oct 03 '19 at 14:19
0

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
Toto
  • 89,455
  • 62
  • 89
  • 125
0

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)
wawa
  • 4,816
  • 3
  • 29
  • 52