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
paulng
Forum Commoner
Posts: 28 Joined: Mon Jul 11, 2005 9:31 am
Post
by paulng » Mon Jul 17, 2006 11:56 am
Hello,
I have a string as follow
Code: Select all
<div id=\"TEXT_CODE\"> </div> and Im trying to strip off everything except TEXT_CODE.
I have tried a few php predefined functions such as strip_tags, str_replace but Im not satisfy with the result...
does anyone know any better function that will give me the result I want...?
Many Thanks in advance.
Paulng.
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Mon Jul 17, 2006 11:58 am
you'll probably need a regular expression to do that.
I assume TEXT_CODE will be a dynamic value that you need to retrieve?
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Jul 17, 2006 12:01 pm
Doing what you are asking will return the value for "id" in all <div id=" strings you find in the given string. Is that what you are looking to do?
paulng
Forum Commoner
Posts: 28 Joined: Mon Jul 11, 2005 9:31 am
Post
by paulng » Mon Jul 17, 2006 12:02 pm
Burrito thanks for the quick reply.
yes TEXT_CODE is a dynamic value but as long as I can get rid of everything around TEXT_CODE my problem will be solve.
Thanks
paulng
Forum Commoner
Posts: 28 Joined: Mon Jul 11, 2005 9:31 am
Post
by paulng » Mon Jul 17, 2006 12:09 pm
eve
yes that's exactly what Im looking for, let me give u this example of something I have tried. I have two variables but I want the result of the two together.
Code: Select all
$testvar = "<div id=\"TEXT_CODE\"> </div>";
$test = str_replace("<div id=\"","","$testvar");
$test2 = str_replace("\"> </div>","","$testvar");
I m not satisfy with this method.
any better solution will be appreciated.
paul
Last edited by
paulng on Mon Jul 17, 2006 12:12 pm, edited 1 time in total.
Burrito
Spockulator
Posts: 4715 Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah
Post
by Burrito » Mon Jul 17, 2006 12:12 pm
try something like this:
untested:
Code: Select all
$string = "<div id=\"TEXT_CODE\"> </div>";
$string = preg_replace("/<div id=\"(.*?)\"> <\/div>/","\\1",$string);
paulng
Forum Commoner
Posts: 28 Joined: Mon Jul 11, 2005 9:31 am
Post
by paulng » Mon Jul 17, 2006 12:21 pm
Burrito,
Thank you very much, it work exactly as I wanted...
Cheers