1

I have a piece of text that contains:

[shipping_address]
<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
  #shipping_streetNrBox#<br>
  #shipping_zipcode# #shipping_city#<br>
  #shipping_country#<br>
</p>
[/shipping_address]

In php if a certain if statements return true, I want to remove the entire block (including [shipping_address][/shipping_address]). I am using a preg_replace but I need some help with the code.

$content = preg_replace("\[shipping_address\](.*?)\[/shipping_address\]", "" , $content);

does not do the trick, can someone help me out please.

2
  • Could you put in the question how you're using preg_replace? It seems that you're not using the delimiters. Commented Sep 16, 2013 at 9:46
  • It sounds like you're trying to write your own templating engine. May I recommend using one that already exists (Smarty, Mustache, Twig...). No point re-inventing the wheel. Commented Sep 16, 2013 at 9:54

5 Answers 5

4

This will do the stuff:

$sData = preg_replace('/\[shipping_address\](.*?)\[\/shipping_address\]/si', '', $sData);

-be aware about using pattern delimiters and multiline replacement (s modifier - in this case, it refers to . (dot) symbol). I've also added i modifier to make replacement case-insensitive.

Sign up to request clarification or add additional context in comments.

5 Comments

The multiline modifier is actually m, but you also need the PCRE_DOTALL modifyer s that will allow . to also match newline characters, but you included both anyway!
@cars10 yes, mistyped - added more brief explanation, thank you
This is most definitely nitpicking, however: you don't need to have a capture group for (.*?) neither do you need the m modifier and you could use a different delimiter to save escaping the / in the closing tag.
Agreed with m (since have no end of lines). Saved () as a part of OP's pattern (perhaps it's used somewhere in replacement in other places - not sure). As for / - it's just a habit (since using that in javascript).
I hear you with / habit... Do it myself all the time haha and when it is only one it doesn't make it all that much of a problem. As for () I see where you're coming from but remain unconvinced :P Still +1 from me as the best answer here
1

You should use Pattern Modifiers.

s (PCRE_DOTALL): If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.

<?php
$string = '123 [shipping_address]

<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
  #shipping_streetNrBox#<br>
  #shipping_zipcode# #shipping_city#<br>
  #shipping_country#<br>
</p>
[/shipping_address] test';


var_dump( preg_replace('/\[shipping_address\].*\[\/shipping_address\]/s', '', $string ));

Comments

0

You can try this

preg_replace('/([shipping_address[^>]*])(.*?)([\/shipping_address])/i', '', $string);

If you want to remove the shippingaddress too: then try this

preg_replace('/[shipping_address[^>]*].*?[\/shipping_address]/i', '', $string);

2 Comments

This will not work since by default, . does not include line breaks. You need to add s modifier
This is not an answer... It will not work and there are a number of different things wrong with it
0

It should work for you:

$content = preg_replace("/\[shipping_address\](.*[\n\r].*)*\[/shipping_address\]/", "" , $content);

Comments

0

You can try this:

$search = "/\[shipping_address\](.*?)\[\/shipping_address]/s";
$replace = " ";
$string = "[shipping_address]
<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
  #shipping_streetNrBox#<br>
  #shipping_zipcode# #shipping_city#<br>
  #shipping_country#<br>
</p>
[/shipping_address]";
echo preg_replace($search,$replace,$string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.