homepage Welcome to WebmasterWorld Guest from 54.226.235.222
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Editing a string
whatson




msg:4457230
 6:23 am on May 24, 2012 (gmt 0)

If I have e.g. 'category.php'
what is the best way to create a variable of just
$variable = 'category'?

Do I have to explode and implode it?

 

Andy Langton




msg:4457323
 11:15 am on May 24, 2012 (gmt 0)

A bit more info would help :)

Usually you would pass URL parameters via GET, perhaps handled by your app index file, e.g.

/index.php?type=category

You might then use a switch statement [php.net] to decide what to do based on the 'type'.

However, if you simply want to grab the 'name' of a PHP file, I would use regex:

<?php
# get the filename = REQUEST_URI is also an option, but maybe less secure?
$filename=$_SERVER['SCRIPT_FILENAME'];
# match the part before .php
preg_match('#.*/([^\.]+).php$#',$filename,$matches);
$filename_part=$matches[1];
# test output
echo "<h1>$filename_part</h1>";
?>


I'm no PHP guru though!

rocknbil




msg:4457444
 3:59 pm on May 24, 2012 (gmt 0)

It also might be more helpful to describe **why** you need to do this, often there are solutions found different than the one you've chosen.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved