You need to convert BB tags [hide_guest][/hide_guest] , I do this, but everything is the same:

 $parsered = 'Текст [hide_guest]Скрыто[/hide_guest] Текст'; $text = preg_replace_callback('#\[hide_guest\](.+?)\[/hide_guest\]#si', 'hide_guest', $parsered); function hide_guest($match) { return '<div class="spoiler">'.$match[1].'</div>'; } echo $text; 

Closed due to the fact that off-topic participants are Visman , Bald , cheops , user194374, zRrr Jul 22 '16 at 9:17 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Visman, Bald, cheops, Community Spirit, zRrr
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    and what on output you want to get? your code gives me Текст <div class="spoiler">Скрыто</div> Текст - strangeqargo
  • 2
    @strangeqargo, funny, but made changes to the question and the code earned) - Rammsteinik
  • Then delete the question. - user207618 9:40 pm
  • @Rammsteinik, in your example, the preg_replace_callback function preg_replace_callback not needed, just enough preg_replace . - Visman
  • moreover enough is enough and str_replace - Naumov

1 answer 1

And what str_replace does not suit you? It will work much faster. This is how it should work correctly:

 $text = str_replace('[hide_guest]', '<div class="spoiler">', $parsered); $text = str_replace('[/hide_guest]', '</div>', $text); 

And it works faster than regular expressions.

  • If I write the bb code in capital letters, then your replacement will stop working. - Visman
  • @Visman, the fact is that bb code is often inserted into js, ​​and the person writes according to the insertion anology, or there is a brief help saying [hide_guest] for example. And the people who will write in big letters with a specialist will see that the code did not work and edit the message. - Naumov