Welcome to WebmasterWorld Guest from 54.166.224.46

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Editing a string

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

WebmasterWorld Senior Member 10+ Year Member



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?
11:15 am on May 24, 2012 (gmt 0)

WebmasterWorld Senior Member andy_langton is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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!
3:59 pm on May 24, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month