Page 1 of 1
help with php strip function
Posted: Mon Jul 17, 2006 11:56 am
by paulng
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.
Posted: Mon Jul 17, 2006 11:58 am
by Burrito
you'll probably need a regular expression to do that.
I assume TEXT_CODE will be a dynamic value that you need to retrieve?
Posted: Mon Jul 17, 2006 12:01 pm
by RobertGonzalez
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?
Posted: Mon Jul 17, 2006 12:02 pm
by paulng
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
Posted: Mon Jul 17, 2006 12:09 pm
by paulng
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
Posted: Mon Jul 17, 2006 12:12 pm
by Burrito
try something like this:
untested:
Code: Select all
$string = "<div id=\"TEXT_CODE\"> </div>";
$string = preg_replace("/<div id=\"(.*?)\"> <\/div>/","\\1",$string);
Posted: Mon Jul 17, 2006 12:21 pm
by paulng
Burrito,
Thank you very much, it work exactly as I wanted...
Cheers
