N
N
Netscout2015-10-15 15:16:03
PHP
Netscout, 2015-10-15 15:16:03

How to remove the content of curly braces in php?

Hello !
Please tell me - how to remove the contents of curly braces in php along with brackets.
For example, there is a variable

$str='текст_полезный{{текст_текст{{текст_внутри скобки}}дальше_текст}}_снова полезный текст';

You need to get at the output: text_useful_again useful text
That is, delete taking into account nested tags.
I wrote a function, but its logic turned out to be incorrect.
function del_figurn ($stroka)

{
  
    $p1=strpos($stroka, '{{');
  $p2=strpos($stroka, '}}');
  
  // строка {{ ... }}   без кавычек
  $podstroka = substr($stroka, $p1+2, $p2-$p1);
  
  
  //echo ($podstroka.'<br><br>');

  
  if (strpos($podstroka, '{{')!==false){
    $pppos=strpos($podstroka, '{{')+1;
      $vnutr=substr($podstroka, $pppos-1, $p2-$p1);
  
  
  $stroka=str_replace ($vnutr, '', $stroka);
                    
                       } else $stroka=substr_replace($stroka,'', $p1, ($p2-$p1)+2);


                    //echo($stroka);
  
  return($stroka);
  
}

In case you come across this code: $str='text_useful{{text_text{{text_inside bracket}}next_text}}_again useful text{{text}}next text';
it will remove "_useful text again" Instead
it should just remove the {{...}} blocks
Thanks for the tips!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2015-10-15
@Netscout

$str = "текст_полезный{{текст_текст{{текст_внутри скобки}}дальше_текст}}_снова полезный текст"; 
$str = preg_replace("/\\{.+\\}/m","",$str);

A
Alexey Skobkin, 2015-10-15
@skobkin

Regular expressions and the preg_replace() function will come to your rescue.

D
Dmitry, 2015-10-15
@mytmid

$str='текст_полезный{{текст_текст{{текст_внутри скобки}}дальше_текст}}_снова полезный текст';
$result = preg_replace('!{{.*}}!u', '' , $str);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question