By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

strip tags with contents n times

P: n/a
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
beginning.

$someBigString="<someTag>someString1</someTag><someTag>someString2</someTag><someTag>someString3</someTag>";

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.
May 22 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hm, you mean like
---
$n=2;
preg_replace("'\<[^\>]*\>[^\<]*\<\/[^\>]*\>'si", "", $someBigString, $n);
---
?

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

Or did I misunderstand you here...

Peter Parker schrieb:
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
beginning.

$someBigString="<someTag>someString1</someTag><someTag>someString2</someTag><someTag>someString3</someTag>";

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.

May 22 '07 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peter Parker wrote:
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
beginning.
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 <htmlpurifier.org Anti-XSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGU0CrqTO+fYacSNoRApiPAJ9FTMMhkPXj/3GGy5AMNKfoANbofACfaj/6
Pb8sEhZ7CjVfPfa3dVP97dg=
=qrwe
-----END PGP SIGNATURE-----
May 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.