problem with preg_match to get title of current page

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
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

problem with preg_match to get title of current page

Post by iansane »

Hi,
I'm working on a script that creates another script. I have that part working, making another php file which includes a template.

The template needs to pull some data from the data base but needs to know the name of the program. The program name happens to be the title of the script that I created. So I am trying to use preg_match to have the script read it's self and find the title.

I'm getting the error, preg_match(): Compilation failed: nothing to repeat at offset 8

Here's my code.

Code: Select all

//get account name from title tag set when program created
$fh = fopen($DOCUMENT_ROOT.$PHP_SELF,"r");
$found=false;
$title="";
$matches = "";
while(!feof($fh) && !$found){
if(preg_match("/<title>(*)<&#47;title>/",fgets($fh,4096),$matches)){
$found=true;
$title=$matches[1];
}
}
fclose($fh);
if($found){
// Here title is correct
echo $title;
}
else{
// this page doesn't contain <title>
}
I originally tried preg_match("/<title>(*)<\\\/title>/",fgets($fh,4096),$matches
which I think was supposed to escape the "/" before title but it gave me a undefined error for "t" so I decided to try the ascii code for "/"

Can anyone suggest what is causing the problem here?

Thanks
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

Re: problem with preg_match to get title of current page

Post by iansane »

Looking at this post I put my pattern and subject into variables and it fixed part of the problem.
viewtopic.php?f=1&t=120786

I still was getting nothing for a title though.

I put it into this little script for testing along with the included template page and had to reboot my computer because it filled the page with errors and wouldn't stop.

Code: Select all

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
?>
<html><head><title>test</title></head><body><?php

$fh = fopen($DOCUMENT_ROOT.$PHP_SELF,"r");
$found=false;
$title="";
$matches = "";
$pattern = '/<title>(*)<\\\/title>/';
$subject = fgets($fh,4096);

while(!feof($fh) && !$found){
if(preg_match($pattern, $subject, $matches)){
$found=true;
$title=$matches[1];
}
}
fclose($fh);
if($found){
// if title found
echo $title;
}
else{
//if title not found
echo "no title found";
}

include('../../templates/enroll1.tpl.php');?>

</body></html>
googleing around and I found how to use $_SERVER['PHP_SELF'] and explode() to get what I need but I'd like to know if anyone sees what I was doing wrong with the above.

Below is the what I ended up doing to get the name I needed.

Code: Select all

<?php

$fileName = $_SERVER['PHP_SELF'];

$fileStr = explode('.', $fileName);
$subStr = explode('/', $fileStr[0]);

echo $subStr[1];

//gives me the name of the file without the "/" in front of it and without the extension.
?>
This was so much easier and less code but the first method of having it read it's self to look for the title tag seems like it would work if I got the code right.


Thanks :D
Post Reply