Finding and Replacing text within Span Tags

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauch
Forum Newbie
Posts: 1
Joined: Wed Mar 09, 2005 12:20 am

Finding and Replacing text within Span Tags

Post by lauch »

I'm sorry if this has been mentioned before, but I was not able to find it if it was.

Basically, I have several strings which may or may not contain a <span> tag enclosing text. (i.e. <span>some Text</span>). What I'd like to do, is find that text, if it exists (if it doesn't then exit the function), and add another span onto the end of it.

basically turn this: <span>some text</span>

into this: <span>some text</span><span class="addedText">some text</span>.

I'm really new to php and have gone through many tutorial sites. My thought is to use strpos to find where span begins and where it ends, then replace it with extra span and return the new string. (I hope it's not too confusing)

I'm wanting to use it for WordPress (if you're familiar) to construct dynamic pullquotes for text I specify without having to write the text twice.

Thank you in advance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_replace() or preg_replace() can do this, depending on the flexibility you need. Read through the Useful Posts thread for information on how the regular expression patterns work so you can build the pattern you need.
Post Reply