Re: eregi not working???

From: Date: Tue, 20 Mar 2001 23:51:01 +0000
Subject: Re: eregi not working???
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-44851@lists.php.net to get a copy of this message
On Wed, 21 Mar 2001 08:46, Clayton Dukes wrote: > > I have a script that looks for <title> tags, but it doesn't seem to be > picking up the titles, can anyone tell me why? > > > Almost all of the web pages look like: > > <? > if(!isset($mainfile)) { include("mainfile.php"); } > if(!isset($headers_sent)) { include("header.php"); } > ?> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> > <HTML> > <HEAD> > <TITLE>Some Title</TITLE> > <META NAME="Author" CONTENT="Some Name"> > </HEAD> > <BODY> > > > Here's my function: > > function getTitle(&$doc) > { > if (eregi("<title>(.*)</title>", $doc, $titlematch)) > $title = trim(eregi_replace("[[:space:]]+", " " , > $titlematch[1])); else > $title = ""; > if ($title == "") > $title = "Untitled Document"; > return $title; > } > > it runs okay... > But, when I use the function, everything is coming back as "Untitled > Document". > > Any suggestions? > > > Clayton Dukes Can't tell you what your problem is, but FWIW here is what I use to do the job - if you can extract some useful bits from that.... <?php function get_title($target) { /* This little function will open the file passed in as $target and extract the TITLE tag and the text contained therein. If nothing is found it returns a default value. */ $contents = File($target); $exists=0; $title = ""; for($i=0; $i < count($contents);$i++){ if(eregi("<TITLE>", $contents[$i])): $exists=true; endif; if($exists): $title .= $contents[$i]; endif; if(eregi("</TITLE>", $contents[$i])): break; endif; } /* End FOR */ /* Get rid of end of line in case title tag is spread over multiple lines */ $title = eregi_replace(10, "", $title); $title = eregi_replace(13, "", $title); if( (empty($title)) || (strtoupper($title) == "<TITLE></TITLE>")): $title = '<TITLE>Research Centre for Injury Studies</TITLE>'; endif; /* Clear the slashes, in case there are wonky chars in the title (Thanks, James!) */ return stripslashes($title); } /* End function get_title */ ?> -- David Robley | WEBMASTER & Mail List Admin RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/ AusEinet | http://auseinet.flinders.edu.au/ Flinders University, ADELAIDE, SOUTH AUSTRALIA

« previous php.general (#44851) next »