wildcard added to string

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
McRed
Forum Newbie
Posts: 3
Joined: Thu Jan 22, 2009 3:25 pm

wildcard added to string

Post 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?
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: wildcard added to string

Post by Popcorn »

instead of messing with $data[0], don't you wanna cut the end off of $page?
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: wildcard added to string

Post 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;
    }
}
 
?>
McRed
Forum Newbie
Posts: 3
Joined: Thu Jan 22, 2009 3:25 pm

Re: wildcard added to string

Post 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?
McRed
Forum Newbie
Posts: 3
Joined: Thu Jan 22, 2009 3:25 pm

Re: wildcard added to string

Post 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];
    }
}
 
?>
 
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Re: wildcard added to string

Post 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.
Post Reply