What would be the best, simplest way to code this: I have a php document that gets most of the page requests (set up in routes config, using code igniter framework) and depending on the uri i want to show the user different content. Example:
http: // domain.tld/2010/
Should show one type of content
http: // domain.tld/2010-nov/
should show another type of content
http: // domain.tld/2010-nov-10/
should show yet another type of content
http: // domain.tld/2010-nov-10-blog-post-title/
should show once again another type of content
Everything else should be treated as if is a product, example:
http: // domain.tld/light-bulb/
and if such a product doesnt exist, its a 404
Below is the code I got at the moment, but I feel it is way too messy. Any suggestion how to make it simpler and more effective? (Tried to get it formatted correctly here but it seem a bit tricky to get code to align properly, a apologize)
Regards, Jason
(had to add spaces in all my urls here because im new and not allowed to post that many urls)
$val is the uri (/2010-nov.......)
function show_what($val){
$arr=array("jan"=>01,"feb"=>02,"mar"=>03,"apr"=>04,"may"=>05,"jun"=>06,"jul"=>07,"aug"=>08,"sep"=>09,"oct"=>10,"nov"=>11,"dec"=>12);
// first check to see if the uri starts with a year (4 digits)
if(is_int((int)substr($val,0,4)) && (int)substr($val,0,4)!=0){
// Get all posts for specified YEAR
if(strlen($val)==4){
// Show all blog posts for specified year
// example: http: // domain.tld/2010/
// Get all posts for specified YEAR and MONTH
}elseif(strlen($val)==8 && substr($val,4,1)=="-" && array_key_exists(substr($val,5,3),$arr)){
// show all blog posts for specified year and month
// example: http: // domain.tld/2010-nov/
// Get all posts for specified YEAR, MONTH and DAY OR! get specified post
}elseif(strlen($val)>=11 && substr($val,4,1)=="-" && array_key_exists(substr($val,5,3),$arr) && substr($val,8,1)=="-" && is_int((int)substr($val,9,2)) && (int)substr($val,9,2)!=0){
// Get all posts for specified YEAR, MONTH and DAY
if(strlen($val)==11){
// show all blog posts for specified year, month and day
// example: http: // domain.tld/2010-nov-10/
// Get specified post
}elseif(substr($val,11,1)=="-"){
// show specified post or 404
// example: http: // domain.tld/2010-nov-10-blog-post-title/
}else{
// "Not a valid article url<Br/>";
// example: http: // domain.tld/2010-nov-10there-is-a-dash-missing-after-day/
}
}else{
// 404, not a real date
}
}else{
// show product with current uri or if it doesnt exist, 404.
}
}
you can simple explode it to array
$array = explode('-',$val);
and make an switch case of the array size like
switch(count($array){
# is like 2010
case 1:
// Show all blog posts for specified year
// example: http: // domain.tld/2010/
$year = $array[0];
break;
.....
}
Split the part on hyphens, like this:
$parts = explode('-', $url)
if (count($parts) == 2) {
$year = $parts[0];
$month = $parts[1];
} else if (count($parts) == 3) {
etc.
I'm not a PHP guy and wouldn't actually know how to implement it on PHP but you should definitely look for URL Rewrite with mod_rewrite in Apache or URL Rewrite in IIS 7 and take advantage of Regular Expressions so you don't need to parse strings.
You can use regular expressions to parse the URL part. For example:
(?<Year>[0-9]{4})
(-
(?<Month>[a-zA-Z]+)
(-
(?<Day>[0-9]{1,2})
(-
(?<Slugg>.*)
)?
)?
)?
(Almost reminds you of Lisp, doesn't it?)
Depending upon which parts are present and valid, perform the appropriate logic.
Here's a tested example to get you started. It includes my regexp solution and a solution using string splitting as suggested by others.
<?php
function getParts($source) {
$re = '/^
(?<Year>[0-9]{4})
(-
(?<Month>[a-zA-Z]+)
(-
(?<Day>[0-9]{1,2})
(-
(?<Slugg>.*)
)?
)?
)?
$/';
$re = str_replace(array(' ', "
", "", "\t"), '', $re); // Strip whitespace that made the RE readable
$matches = null;
if (!preg_match($re, $source, $matches)) {
return array('title' => $source);
}
$ret = array();
if (!$matches['Year']) {
return $ret;
}
$ret['year'] = (int) $matches['Year'];
if (!$matches['Month']) {
return $ret;
}
$monthToNumber = array('jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' =>>
$monthName = strtolower($matches['Month']);
if (!array_key_exists($monthName, $monthToNumber)) {
return $ret;
}
$ret['month'] = $monthToNumber[$monthName];
if (!$matches['Day']) {
return $ret;
}
$ret['day'] = (int) $matches['Day'];
if (!$matches['Slugg']) {
return $ret;
}
$ret['title'] = $matches['Slugg'];
return $ret;
}
function getParts2($source) {
$ret = array();
$errorRet = array('title' => $source);
$rawParts = explode('-', $source, 4);
if (count($rawParts) < 1 || !is_numeric($rawParts[0])) {
return $errorRet;
}
$ret['year'] = (int) $rawParts[0];
if (count($rawParts) < 2) {
return $ret;
}
$monthToNumber = array('jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' =>>
$monthName = strtolower($rawParts[1]);
if (!array_key_exists($monthName, $monthToNumber)) {
return $errorRet;
}
$ret['month'] = $monthToNumber[$monthName];
if (count($rawParts) < 3) {
return $ret;
}
$ret['day'] = (int) $rawParts[2];
if (count($rawParts) < 4) {
return $ret;
}
$ret['title'] = $rawParts[3];
return $ret;
}
function test($testFunc, $source, $expected) {
$actual = call_user_func($testFunc, $source);
if ($actual !== $expected) {
echo "Test failed;
";
echo "Input: ";
var_dump($source);
echo "Actual: ";
var_dump($actual);
echo "Expected: ";
var_dump($expected);
}
}
foreach (array('getParts', 'getParts2') as $testFunc) {
test($testFunc, '2010', array('year' => 2010));
test($testFunc, '2010-nov', array('year' => 2010, 'month' => 11));
test($testFunc, '2010-nov-10', array('year' => 2010, 'month' => 11, 'day' => 10));
test($testFunc, '2010-nov-10-blog-post-title', array('year' => 2010, 'month' => 11, 'day' => 10, 'title' => 'blog-post-title'));
test($testFunc, 'light-bulb', array('title' => 'light-bulb'));
}
You should really take a look at the CodeIgniter Framework. Things like URL ReWrite and such are all build into the Framework and it's easy to use + lightning fast. I'm using it since 2008.