Re: so nobody knows how do get rid of everything outside or certain tags?

From: Date: Fri, 16 Aug 2002 03:36:25 +0000
Subject: Re: so nobody knows how do get rid of everything outside or certain tags?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-112903@lists.php.net to get a copy of this message
Here si some code that I used YEARS ago, and I think it was posted to this list for me in the first place. It was designed to grab some pseudo mark language from a text file like: <TITLE>An article about PHP</TITLE> <TEXT> This is my article text </TEXT> <FOOTER>This is my footer</FOOTER> <COPYRIGHT>Copyright me 2002</COPYRIGHT> AI have NO IDEA how is works with multiple occurrences of one of the tags, but in theory, you should be able to massage this code to look for a DIV, or even DIV id=xxx. <? $file = "data/something/blah.txt"; if(file_exists("data/products/".$id.".cont")) { $content = join( '', file('data/products/'.$id.'.cont')); $content = ereg_replace("\n","",$content); $content = ereg_replace("\t","",$content); $content = ereg_replace("\r","",$content); } $tags = array( 'TITLE' => '', 'TEXT' => '', 'FOOTER' => '', 'COPYRIGHT' => '' ); for (reset($tags); $tag = key($tags); next($tags)) { if (ereg('<'.$tag.'>(.*)</'.$tag.'>', $content, $patterns)) { $tags[$tag] = $patterns[1]; } } echo "<B>{$tags['TITLE']}</B><BR>{$tags['COPYRIGHT']}<BR><BR>"; echo "{$tags['TEXT']}<BR><BR>{$tags['FOOTER']}"; ?> At the very least, this should be proof of concept that ereg WILL probably do what you want. Even taking just a snippet from it, We can see that you might easily get what you want: <? $content = "This is my <DIV>favourite thing</DIV> to do"; if(ereg('<DIV>(.*)</DIV>', $content, $patterns)) { $divContent = $patterns[1]; } echo $divContent; ?> This should echo "favourite thing". Have fun, Justin on 16/08/02 2:44 AM, Jay Blanchard (jay.blanchard@niicommunications.com) wrote: > [snip] > I asked this question a while ago, and I am still seeking clarification, > I fooled with ereg_replace and they only do a piece of what I want, I > would like to know how to keep only a chunk of text defined between 2 > tags let's say, <div> and </div> and ignore everything else, I also have > to search and replace certain tags between, but I know how to do that > with ereg_replace. So can anyone help me echo only what is between the > div tags? Please? > [/snip] > > Why not take the the beginning <div>, everything in the middle, and the end > </div> tag and explode() them into an array and then get what you want and > write it out? I didn't see your originl post and don't know if there was > more detail available, but I hope this helps. > > Jay > > I’m not tense…just terribly, terribly alert > > *********************************************************** > * Texas PHP Developers Conf Spring 2003 * > * T Bar M Resort & Conference Center * > * New Braunfels, Texas * > * San Antonio Area PHP Developers Group * > * Interested? Contact jay.blanchard@niicommunications.com * > *********************************************************** > >

« previous php.general (#112903) next »