Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更好的编码方式?_Php - Fatal编程技术网

Php 更好的编码方式?

Php 更好的编码方式?,php,Php,编写此代码的最佳、最简单的方法是: 我有一个php文档,它获取大多数页面请求(在routes config中设置,使用code igniter框架),并根据uri向用户显示不同的内容。例如: 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

编写此代码的最佳、最简单的方法是: 我有一个php文档,它获取大多数页面请求(在routes config中设置,使用code igniter框架),并根据uri向用户显示不同的内容。例如:

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
其他一切都应视为产品,例如:
http://domain.tld/light bulb/
如果这样的产品不存在,那就是404

下面是我目前得到的代码,但我觉得太乱了。有什么建议可以让它更简单更有效吗?(此处尝试将其正确格式化,但要使代码正确对齐似乎有点棘手,抱歉)

问候,, 杰森
(必须在我的所有URL中添加空格,因为我是新的,不允许发布那么多URL)

$val是uri(/2010年11月……)
函数show_what($val){
$arr=数组(“一月”=>01,“二月”=>02,“三月”=>03,“四月”=>04,“五月”=>05,“六月”=>06,“七月”=>07,“八月”=>08,“九月”=>09,“十月”=>10,“十一月”=>11,“十二月”=>12);
//首先检查uri是否以年(4位数字)开头
如果(is_int((int)substr($val,0,4))&(int)substr($val,0,4)!=0){
//获取指定年份的所有职位
如果(strlen($val)==4){
//显示指定年份的所有博客文章
//示例:http://domain.tld/2010/
//获取指定年份和月份的所有职位
}elseif(strlen($val)==8&&substr($val,4,1)=“-”&&array_key_存在(substr($val,5,3),$arr)){
//显示指定年份和月份的所有博客文章
//示例:http://domain.tld/2010-nov/
//获取指定年、月、日的所有帖子或!获取指定帖子
}elseif(strlen($val)>=11&&substr($val,4,1)=“-”&&array_key_存在($substr($val,5,3),$arr)&&substr($val,8,1)=“-”&&is_int((int)substr($val,9,2))&(int)substr($val,9,2)!=0){
//获取指定年、月、日的所有帖子
如果(strlen($val)==11){
//显示指定年份、月份和日期的所有博客文章
//示例:http://domain.tld/2010-11-10/
//获得指定职位
}elseif(substr($val,11,1)==“-”){
//显示指定的post或404
//示例:http://domain.tld/2010-nov-10-blog-post-title/
}否则{
//“不是有效的文章url
”; //示例:http://domain.tld/2010-nov-10there-is-a-dash-missing-after-day/ } }否则{ //404,不是真正的约会 } }否则{ //用当前uri显示产品,如果不存在,则显示404。 } }
您可以简单地将其分解为数组

$array = explode('-',$val);
并制作一个数组大小的开关盒,如

   switch(count($array){
    # is like 2010
    case 1:
       // Show all blog posts for specified year
       // example: http: // domain.tld/2010/
       $year = $array[0];
    break;
    .....
    }

您可以简单地将其分解为数组

$array = explode('-',$val);
并制作一个数组大小的开关盒,如

   switch(count($array){
    # is like 2010
    case 1:
       // Show all blog posts for specified year
       // example: http: // domain.tld/2010/
       $year = $array[0];
    break;
    .....
    }

在连字符上拆分零件,如下所示:

$parts = explode('-', $url)
if (count($parts) == 2) {
    $year = $parts[0];
    $month = $parts[1];
} else if (count($parts) == 3) {

等等。

在连字符上拆分零件,如下所示:

$parts = explode('-', $url)
if (count($parts) == 2) {
    $year = $parts[0];
    $month = $parts[1];
} else if (count($parts) == 3) {

等等。

我不是一个PHP爱好者,实际上不知道如何在PHP上实现它,但你肯定应该在Apache中使用mod_Rewrite进行URL重写,或者在IIS 7中使用URL Rewrite,并利用正则表达式,这样你就不需要解析字符串。

我不是一个PHP爱好者,也不知道如何在PHP上实现它,但你知道应该在Apache中使用mod_Rewrite进行URL重写,或者在IIS 7中使用URL重写,并利用正则表达式,这样就不需要解析字符串。

您可以使用正则表达式解析URL部分。例如:

(?<Year>[0-9]{4})
(-
  (?<Month>[a-zA-Z]+)
  (-
    (?<Day>[0-9]{1,2})
    (-
      (?<Slugg>.*)
    )?
  )?
)?
(?[0-9]{4})
(-
(?[a-zA-Z]+)
(-
(?[0-9]{1,2})
(-
(?.*)
)?
)?
)?
(几乎让你想起口齿不清,不是吗?)

根据存在和有效的部件,执行适当的逻辑

下面是一个经过测试的示例,让您开始学习。它包括我的regexp解决方案和其他人建议的使用字符串拆分的解决方案

<?php

function getParts($source) {
    $re = '/^
    (?<Year>[0-9]{4})
    (-
      (?<Month>[a-zA-Z]+)
      (-
        (?<Day>[0-9]{1,2})
        (-
          (?<Slugg>.*)
        )?
      )?
    )?
    $/';

    $re = str_replace(array(' ', "\n", "\r", "\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;\n";
        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'));
}

您可以使用正则表达式来解析URL部分。例如:

(?<Year>[0-9]{4})
(-
  (?<Month>[a-zA-Z]+)
  (-
    (?<Day>[0-9]{1,2})
    (-
      (?<Slugg>.*)
    )?
  )?
)?
(?[0-9]{4})
(-
(?[a-zA-Z]+)
(-
(?[0-9]{1,2})
(-
(?.*)
)?
)?
)?
(几乎让你想起口齿不清,不是吗?)

根据存在和有效的部件,执行适当的逻辑

下面是一个经过测试的示例,让您开始学习。它包括我的regexp解决方案和其他人建议的使用字符串拆分的解决方案

<?php

function getParts($source) {
    $re = '/^
    (?<Year>[0-9]{4})
    (-
      (?<Month>[a-zA-Z]+)
      (-
        (?<Day>[0-9]{1,2})
        (-
          (?<Slugg>.*)
        )?
      )?
    )?
    $/';

    $re = str_replace(array(' ', "\n", "\r", "\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;\n";
        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'));
}

您真的应该看一下。URL重写之类的东西都内置到框架中,使用起来非常简单,而且速度非常快。我从2008年就开始使用它了。

你真的应该看看它。URL重写之类的东西都内置到框架中,使用起来非常简单,而且速度非常快。我从2008年开始使用它。

+1 URL重写对我来说似乎是最好的选择,这里是一个简单的介绍,+1 URL重写对我来说似乎是最好的选择,这里是一个简单的介绍,已经在使用它并重写URL,并且使用路由将请求发送到适当的文件。我想设置一条适用于我所拥有的不同选项的路由是另一种选择,我只是不知道如何编写它。为什么不简单地将URL分成控制器呢?content/show/desired\u content已经在使用它并重写URL,并且使用路由将请求发送到适当的文件。我想设置一条适用于我所拥有的不同选项的路由是另一种选择,我只是不知道如何编写它。为什么不简单地将URL分成控制器呢?content/show/desired_contentok,我把它的大部分放在CodeIgniter的routes.php文件中怎么样?每个域的适当regexp是什么:domain.tld/2010/domain.tld/2010-nov/domain.tld/2010-nov-10/domain.tld/2010-nov-10-article/I然后可以将它们路由到它们自己的函数,并验证日期和文章是否存在?是否不可能在注释中添加新行?“回答我自己的问题”按钮在我的浏览器中根本不起作用。叹气:(好的,我把它的大部分放在CodeIgniter中的routes.php文件中如何?每个文件的合适regexp是什么:domain.tld/2010/domain.tld/2010-nov/domain.tld/2010-nov-10/domain.tld/2010-nov-10-article/然后我可以将它们路由到它们自己的函数,并验证日期和时间