Page 1 of 1
wildcard added to string
Posted: Thu Jan 22, 2009 3:38 pm
by McRed
I've been trying to get wildcard to function when I use the following bit of code. I'm using an external txt file to store all of my page titles to be called in my header file.
Code: Select all
<?php
$database = 'titletags.txt';
$meta_db = fopen($database, 'r');
$page = $_SERVER['REQUEST_URI'];
$page = substr($page, 1);
while($data = fgetcsv($meta_db, 9000, '|'))
{
if($data[0] == $page)
{
$title = $data[1];
}
}
?>
The titletags.txt is a text file that simple has this text
search.php|your search results
The part I'm having problems with is having a wildcard after $data[0] so that any of my pages like search.php?keywords=something will register the same as search.php. I've tried lots of combinations trying to insert a wildcard after $data[0] but with no luck. Any suggestions?
Re: wildcard added to string
Posted: Fri Jan 23, 2009 10:35 pm
by Popcorn
instead of messing with $data[0], don't you wanna cut the end off of $page?
Re: wildcard added to string
Posted: Sat Jan 24, 2009 4:32 pm
by William
You might want to use basename(__FILE__) instead. Here is an example:
Code: Select all
<?php
$database = file('titletags.txt');
$title = '';
foreach ($database as $row)
{
$fields = explode('|', $row);
if (isset($fields['1']) === true && $fields['0'] == basename(__FILE__))
{
$title = $fields['1'];
break;
}
}
?>
Re: wildcard added to string
Posted: Thu Jan 29, 2009 2:19 pm
by McRed
I tried basename($page).....
for /search.php it would return search.php, however when I used /search.php?keywords=something it would return /search.php?keywords=something, so different method, but i'm still getting the same results. That's not working quite right.
If I can't include a wildcard after my string, can I truncate anything that appears from the . going forward?
Re: wildcard added to string
Posted: Thu Jan 29, 2009 2:45 pm
by McRed
I got it now. I used a combination of what I had originally and then used the explode like you suggested to strip out everything after the . in the URL. I had to alter the text file to compensate for the new format, but it works great now.
Thanks!
Code: Select all
<?php
$database = 'titletags.txt';
$meta_db = fopen($database, 'r');
$page = $_SERVER['REQUEST_URI'];
list($pagename, $rest) = explode(".", $page);
while($data = fgetcsv($meta_db, 9000, '|'))
{
if($data[0] == basename($pagename))
{
$title = $data[1];
}
}
?>
Re: wildcard added to string
Posted: Fri Jan 30, 2009 9:52 am
by William
McRed wrote:I got it now. I used a combination of what I had originally and then used the explode like you suggested to strip out everything after the . in the URL. I had to alter the text file to compensate for the new format, but it works great now.
Thanks!
Code: Select all
<?php
$database = 'titletags.txt';
$meta_db = fopen($database, 'r');
$page = $_SERVER['REQUEST_URI'];
list($pagename, $rest) = explode(".", $page);
while($data = fgetcsv($meta_db, 9000, '|'))
{
if($data[0] == basename($pagename))
{
$title = $data[1];
}
}
?>
In my example I was using __FILE__, not the REQUEST URI. You might not be able to use __FILE__ because of how your include structure is setup, I just wanted to make sure you knew that __FILE__ is a
PHP magic constant, and not me trying to say to put your file name in there.