/    Sign up×
Community /Pin to ProfileBookmark

Search function that searches a html tag can I make it search a PHP variable

I have a function that searches for a html tag <title> using preg_match, but how will I use this to search for a php variable, anything I have tried doesnt work, I think I’m being dumb.

[code=php]
function getTitle($file){
global $htmltypes;
$title=””;
$p=pathinfo($file);
if(!in_array(strtolower($p[‘extension’]),$htmltypes)){
$f=file_get_contents($file);
//if(preg_match(“‘<title>(.+)</title>’i”, $f, $matches)){ //used for html
if(preg_match(“‘/$pagetitle/’i”, $f, $matches)){ //hopefully get it to search a php variable
$title=$matches[1];
}
}
$title=$title?$title:basename($file);
return htmlentities(trim(strip_tags($title)));
}
[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsApr 30.2006 — I believe file_get_contents parses php files before returning them, so that might be your problem.
Copy linkTweet thisAlerts:
@solidaritiauthorApr 30.2006 — If it parses the file then it would just turn it into html anyway, putting the page title in between the <title></title> tags. I asked the author of the code how to modify it to use it with a php site, and he said i would just need to change the preg_match.
Copy linkTweet thisAlerts:
@NogDogApr 30.2006 — I don't see where you're setting the value of $pagetitle, so if it's unset then what you're currently searching for is "//". If the value of $pagetitle is set in the main part of the script, then within the function you'll need to add a [b]global $pagetitle;[/b] before you reference it (or else add it to the parameter list of your function declaration).
Copy linkTweet thisAlerts:
@balloonbuffoonApr 30.2006 — Can you post an example file that you would be searching and what exactly in it you are searching for?

--Steve
Copy linkTweet thisAlerts:
@solidaritiauthorMay 01.2006 — This is the js script that it calls:

[code=php]
//<![CDATA[
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function toggle(l,i){
o=MM_findObj(l);
o.style.display=o.style.display=="none"?"block":"none";
if (document.images){
document.images[i].src = o.style.display=="block"?"www/sites/uwafc/assets/images/sitemap/sitemap_minus.gif":"www/sites/uwafc/assets/images/sitemap/sitemap_plus.gif";
}
}
//]]>
[/code]



This is the code used for the site-map:

[code=php]
<h1>UWA FC Sitemap</h1>

<p>This will generate a tree of UWA FC's website, allowing you
quick access to vital pages and information that you may require.</p>

<?php
// A root relative path to the top level directory you want indexed.
$startin="/www/sites/uwafc/";

// A root relative path to the location where you place the images.
// Do NOT include a trailing slash. Correct usage would be similar to:
$imgpath="/www/sites/uwafc/assets/images";

// If you leave it set to an empty string, the program will assume the
// images are located in the same directory as the script.
$imgpath="/www/sites/uwafc/assets/images/sitemap";

// The $types array contains the file extensions of files you want to
// show in the site map.
$types=array(
".php",
".html",
".htm",
".shtm",
".sthml"
);

// The $htmltypes is an array containing the file types of HTML files,
// that is files that will contain the HTML <title> tag. The script will
// not try to extract the <title> from these files and will simply use
// the document name as the title.
$htmltypes=array(
".php",
".html",
".htm",
".shtm",
".sthml",
".doc",
".xls",
".pdf"
);

// files and/or directories to ignore
$ignore=array(
".htaccess",
"cgi-bin",
"images",
"index.htm",
"index.html",
"index.php",
"robots.txt",
"gallery",
"gallery-data",
"includes",
".js",
".gif",
".jpeg",
"members",
"uwa-old",
"shoutbox"
);

/*
==============================================
You should not need to make changes below here
==============================================
*/
?><div id="sitemap"><?php
$id=0;
echo "<ul id="list$id">n";
$id++;
$divs="";
if(substr($startin,strlen($startin)-1,1)=="/")
$startin=substr($startin,0,strlen($startin)-1);
foreach($types as $type){
if (file_exists($_SERVER['DOCUMENT_ROOT']."$startin/index$type")){
$index=$_SERVER['DOCUMENT_ROOT']."$startin"."/index$type";
break;
}
}
$types=join($types,"|");
$types="($types)";
if(!is_array($htmltypes))
$htmltypes=array();
if(count($htmltypes)==0)
$htmltypes=$types;
if(!$imgpath)
$imgpath=".";
echo "<li><img src="$imgpath/sitemap_server.gif" align="middle" alt="" /><strong><a href="$startin/">".getTitle($index)."</a></strong>n";
showlist($_SERVER['DOCUMENT_ROOT']."$startin");
echo "</li></ul>n";
if (is_array($divs)){
$divs="'".join($divs,"','")."'";
echo "<script type="text/javascript">n";
echo "//<![CDATA[n";
echo "d=Array($divs);n";
echo "for (i=0;i<d.length;i++){n";
echo "ttoggle('list'+d[i],'img'+d[i]);n";
echo "}n";
echo "//]]>n";
echo "</script>n";
}


function showlist($path){
global $ignore, $id, $divs, $imgpath, $types;
$dirs=array();
$files=array();
if(is_dir($path)){
if ($dir = @opendir($path)) {
while (($file = readdir($dir)) !== false) {
if ($file!="." && $file!=".." && !in_array($file,$ignore)){
if (is_dir("$path/$file")){
if (file_exists("$path/$file/index.php"))
$dirs[$file]=getTitle("$path/$file/index.php");
elseif (file_exists("$path/$file/index.html"))
$dirs[$file]=getTitle("$path/$file/index.html");
elseif (file_exists("$path/$file/index.htm"))
$dirs[$file]=getTitle("$path/$file/index.htm");
else
$dirs[$file]=$file;
} else {
if (ereg("$types$", $file)){
$files[$file]=getTitle("$path/$file");
if (strlen($files[$file])==0)
$files[$file]=$file;
}
}
}
}
closedir($dir);
}
natcasesort($dirs);
$url=str_replace($_SERVER['DOCUMENT_ROOT'],"",$path);
$n=substr_count("$url/$","/");
$indent=str_pad("",$n-1,"t");
echo "$indent<ul id="list$id">n";
if ($n>1)
$divs[]="$id";
$imgsrc="sitemap_minus";
foreach($dirs as $d=>$t){
$id++;
echo "$indentt<li><a href="javascript:toggle('list$id','img$id')"><img src="$imgpath/$imgsrc.gif" id="img$id" align="middle" border="0" alt="" /></a>";
echo "<img src="$imgpath/sitemap_folder.gif" alt="" align="middle" />";
echo " <strong><a href="$url/$d/">$t</a></strong>n";
showlist("$path/$d");
echo "$indentt</li>n";
}
natcasesort($files);
$id++;
foreach($files as $f=>$t){
echo "$indentt<li><img style="padding-left:20px;" src="$imgpath/sitemap_html.gif" alt="" border="0" /> <a href="$url/$f">$t</a></li>n";
}
echo "$indent</ul>n";
}
}

function getTitle($file){
global $htmltypes;
$title="";
$p=pathinfo($file);
if(!in_array(strtolower($p['extension']),$htmltypes)){
$f=file_get_contents($file);
//if(preg_match("'<title>(.+)</title>'i", $f, $matches)){ //used for html
if(preg_match("'/$pagetitle/'i", $f, $matches)){
$title=$matches[1];
}
}
$title=$title?$title:basename($file);
return htmlentities(trim(strip_tags($title)));
}
?><?php
[/code]
Copy linkTweet thisAlerts:
@solidaritiauthorMay 01.2006 — Here is the top of every php page the included above-content.php, which has been shortened, due to the length of it:

[code=php]
//set default pagetitle if page value is empty
if (empty($pagetitle)) {
$pagetitle = "UWA FC - The Official Website";
}else{
$uniqueTitle = $pagetitle;
$pagetitle = "UWA FC - ".$uniqueTitle;
}

require('db_connect.php');

$dbQuery = "SELECT rights ";

$dbQuery .= "FROM users WHERE username = ('$_SESSION[username]')";
$result = mysql_query($dbQuery) or die("Couldn't get file list");
while($row = mysql_fetch_array($result))
{

$permission = "$row[rights]"; // get access level
$_SESSION["perm"] = "$permission"; // make session variables
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="eng">
<head>

<title><?=$pagetitle?></title>

<!-- Meta Tags -->
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<meta name="robots" content="index, follow" />

<meta name="description" content="Univeristy Of Wales Aberystwyth Football Club" />
<meta name="keywords" content="Football, Club, winners, uni, students, drink, apps, appsy" />
<meta name="author" content="James Apps, UWA" />
<!-- Favicon -->
<link rel="shortcut icon" href="<?=$path?>/assets/favicon.ico" />

<!-- CSS -->
<link rel="stylesheet" href="<?=$path?>/assets/css/default.css" media="screen,projection" type="text/css"/>
<!--[if lt IE 7]>
<link rel="stylesheet" href="<?=$path?>/assets/css/IEfix.css" media="screen,projection" type="text/css" />
<![endif]-->
<!--[if lte IE 5.5]>
<link rel="stylesheet" href="<?=$path?>/assets/css/IE5fix.css" media="screen,projection" type="text/css" />
<![endif]-->
<link rel="stylesheet" href="<?=$path?>/assets/css/print.css" media="print" type="text/css" />
<!-- If adminpage is called use this style -->
<link href="ALSAS_<?PHP
if (isset($HTTP_POST_VARS['Admin_style'])==true){
echo $HTTP_POST_VARS['Admin_style'];
}else{
//echo getSetting("Admin_style");
}
?>.css" rel="stylesheet" type="text/css">
<!-- niftyCorners -->
<script src="<?=$path?>/assets/scripts/nifty.js" type="text/javascript"></script>
<script src="<?=$path?>/assets/scripts/rounded.js" type="text/javascript"></script>
<!-- Quotes Script : Displays quotes on the page -->
<script src="<?=$path?>/assets/scripts/quotes.js" type="text/javascript"></script>
<!-- Stops the browser from displaying errors -->
<script src="<?=$path?>/assets/scripts/symerror.js" type="text/javascript"></script>
<!-- News Scroller -->
<script src="<?=$path?>/assets/scripts/news.js" type="text/javascript"></script>
<!-- Sitemap -->
<script src="<?=$path?>/assets/scripts/sitemap.js" type="text/javascript"></script>
</head>

<body <?=$bodyid?>>

<ul id="skiplinks">
<li><a href="#navigation" accesskey="n">Skip to navigation</a></li>
<li><a href="#siteimageandinfo" accesskey="s">Skip to Site Title, Image and Logo</a></li>
<li><a href="#content" accesskey="s">Skip to content</a></li>
</ul>

<div id="container">
<div id="header">
<div id="navigation">
<a name="navigation"></a>
<ul>
<li><a href="<?php echo $path ?>/" title="University Of Wales Aberystwyth Football Club" accesskey="1">Home</a></li>
<li<?php echo $menuTeams; ?>><a href="<?php echo $path ?>/teams/" title="Teams" accesskey="2">Teams</a></li>
<li<?php echo $menuPlayerProfiles; ?>><a href="<?php echo $path ?>/player-profiles/" title="Player-Profiles" accesskey="3">Player-Profiles</a></li>
<li<?php echo $menuBusa; ?>><a href="<?php echo $path ?>/busa/" title="Busa : British University Sports Association" accesskey="4">Busa</a></li>
<li<?php echo $menuSocials; ?>><a href="<?php echo $path ?>/socials/" title="Socials" accesskey="5">Socials</a></li>
<li<?php echo $menuBanter; ?>><a href="<?php echo $path ?>/banter/" title="What a load of Banter : UWA FC Forum" accesskey="6">Banter</a></li>
<li<?php echo $menuTraining; ?>><a href="<?php echo $path ?>/training/" title="Training Sessions : Train hard to win easy" accesskey="7">Training</a></li>
<li<?php echo $menuSilverware; ?>><a href="<?php echo $path ?>/silverware/" title="The fruits of our labour" accesskey="7">Silverware</a></li>
<li<?php echo $menuKanesBar; ?>><a href="<?php echo $path ?>/kanes-bar/" title="KANES Bar the Local"><img src="<?=$path?>/assets/images/sponsor_kanesbar.gif" alt="KANES Bar the Local" /></a></li>
<li<?php echo $menuBudweiser; ?>><a href="<?php echo $path ?>/budweiser/" title="Budwesier the Official Sponsor"><img src="<?=$path?>/assets/images/sponsor_budweiser.gif" alt="Budweiser the official sponsor" /></a></li>
</ul>
</div><!-- navigation -->

[/code]


This is a page in the website that declares a $pagetitle:

[code=php]
<?php
ob_start();
include '../includes/news.php';
$contents = ob_get_contents(); // $contents now holds results of include
ob_end_clean();

// Define variables
$pagetitle=""; // default is "UWAFC"
$internal_banner=""; // y or blank - adds top banner to content
$submenu= // define submenu for this page
<<<SUBMENUBLOCK

<ul>
<li class="selected"><a href="index.php">Members Area</li>
<li><a href="news/addnews.php">Add News</li>
<li><a href="team/teamlist.php">List all Teams</li>
<li>$contents</li>
<ul>

SUBMENUBLOCK;
include ("../includes/auto-prepend.php");
include("../includes/above-content.php"); // include header
// content?>


<div>
<!--<div id="homeimage"></div>-->

<div>
<h1>UWA FC Members Area</h1>


<p>
From here you can control
</p>
<p>

</p>

</div>
</div>

<!--<div id="HomepageBanner"></div>-->

<?php include("../includes/below-content.php"); //include footer ?>
[/code]
×

Success!

Help @solidariti spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...