Source:

[display = "home"] html code [/display] [display = "category"] html code [/display] [display = "home"] html code [/display] 

Tell me how to write a regular expression so that the shortcode for example [display = "home"]...[/display] replaced with void. The main thing is that it would be replaced in turn. That is, the regular expression should find the beginning of [display = "..."]... and the first one found [/display] , not the last. I tried to describe clearly.

I do this in a loop, but the result is bad:

 $pattern = str_replace(["\"", "[", "]", " ", "/"], ["\\\"", "\[", "\]", "\s+", "\/"], $display[1][$key]); $pattern2 = str_replace(["\"", "[", "]", " ", "/"], ["\\\"", "\[", "\]", "\s+", "\/"], $display[4][$key]); $result = preg_replace('/'.$pattern.'(.+?)'.$pattern2.'/is', "", $result); 

Where \[display = \"home\"\] falls into $pattern2 , \[\/display\] falls into $pattern2 but in the end the result is not what is expected.

  • Is this result expected? example - Edward
  • it is expected to replace, for example, the second short code and all that is stored in it is empty, but in your case, simply the first short code with the internal code is displayed. But I probably did not understand your code correctly. - Vladimir
  • @ Edward yes, I did not understand right away. Your option works, but I do not understand how to write it in my case. - Vladimir
  • @ Edward by code /\[display\s+=\s+\"category\"\]([^[]+)\[\/display\]/U I have everything normal, but on the main page, when = home , vylazit it [display = "category"] , it is difficult to describe in words. - Vladimir
  • Yeah, right. The variable stores the page type. - Vladimir

1 answer 1

Here you have a regular template template:

 /\[display = "(.+)"\]([^[]+)\[\/display\]/U 

If you use preg_replace_callback you can change the content on the fly or even remove the blocks altogether.
Regulars work example: https://regex101.com/r/WlkqLp/1/

 $regExp = '/\[display = "(.+)"\]([^[]+)\[\/display\]/U'; preg_replace_callback($regExp,function ($matches) { if($matches[1]=='home') return ''; if($matches[1]=='category') return $matches[2]; },$value); 
  • @ Edward is more or less better, but everything is around. Sorry, can't I ask skype for online help? - Vladimir
  • @ Edward and what is wrong? the regular is not greedy and chooses exactly what the author requested, it is even written exactly how to use it for its purposes, and now it is also supplemented by an example .... - Vladimir Klykov
  • one
    g read mang on regex for php :) g in php is not supported)) - Edward
  • one
    @ Vladimir thanks to Eduard for the sandbox, here’s an example of how to use sandbox.onlinephpfunctions.com/code/… for chat - you need to ask the moderator to create this =) I don’t have enough rights to create a chat in comments about g - in php and without it It will work fine, and it doesn’t bother it there, but if it doesn’t work in php, which is most likely the case, it doesn’t interfere there =) - Vladimir Klykov
  • one
    @Vladimir Klykov why do you mislead a person? ) The g modifier is not written - it means it will not work: Warning: preg_replace(): Unknown modifier 'g' - Edward