help with php strip function

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
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

help with php strip function

Post by paulng »

Hello,

I have a string as follow

Code: Select all

<div id=\"TEXT_CODE\">&nbsp;</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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

Post 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
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

Post 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\">&nbsp;</div>";



 $test = str_replace("<div id=\"","","$testvar");
   $test2 = str_replace("\">&nbsp;</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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try something like this:

untested:

Code: Select all

$string = "<div id=\"TEXT_CODE\">&nbsp;</div>";
$string = preg_replace("/<div id=\"(.*?)\">&nbsp;<\/div>/","\\1",$string);
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

Post by paulng »

Burrito,


Thank you very much, it work exactly as I wanted...


Cheers :D
Post Reply