strip tags with contents n times

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
What is the best way to delete tags n times from either the beginning or the  
end of a String? For example, to delete "someString1" and "someString2"  
along with surrounding <someTag>'s with n=2 and processing direction is from  


I was thinking about use preg_replace() to replace "/</tag><tag/" with ""  
but wasn't sure how, especially about the n times and direction of  
processing. Any suggestion? Is using prreg_replace() the best way to  
accomplish this task? Thanks.  

Re: strip tags with contents n times

Hm, you mean like
preg_replace("'\<[^\>]*\>[^\<]*\<\/[^\>]*\>'si", "", $someBigString, $n);

Alternatively, if you'd like to limit the Tag to a specific one you  
could also do
preg_replace("'\<someTag\>[^\<]*\<\/someTag\>'si", "", $someBigString, $n);

Or did I misunderstand you here...

Peter Parker schrieb:
Quoted text here. Click to load it

Re: strip tags with contents n times

Hash: SHA1

Peter Parker wrote:
Quoted text here. Click to load it

If you're dealing with valid XML, it may be a good idea to parse it
using one of PHP's XML extensions, do the manipulations you want with
DOM/SimpleXML API, and then serialize it back to XML.

preg_replace will only work from the front, although you can use the
$limit parameter to get the "n times" functionality.

- --
 Edward Z. Yang                        GnuPG: 0x869C48DA
 HTML Purifier  <>  Anti-XSS HTML Filter
 [[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla -


Site Timeline