php - preg_match语法

标签 php codeigniter

研究了 codeigniter tut 中的一些代码,以下 preg_match 模式让我感到困惑:

preg_match('/js$/', $include)

js后面的$的作用是什么?

感谢您始终周到的回复!

-----完整代码-----

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Layouts Class. PHP5 only.
 *
 */
class Layouts {

  // Will hold a CodeIgniter instance
  private $CI;

  // Will hold a title for the page, NULL by default
  private $title_for_layout = NULL;

  // The title separator, ' | ' by default
  private $title_separator = ' | ';

  public function __construct()
  {
    $this->CI =& get_instance();
  }

  public function set_title($title)
  {
    $this->title_for_layout = $title;
  }

  public function view($view_name, $params = array(), $layout = 'default')
  {
    // Handle the site's title. If NULL, don't add anything. If not, add a
    // separator and append the title.
    if ($this->title_for_layout !== NULL)
    {
      $separated_title_for_layout = $this->title_separator . $this->title_for_layout;
    }

    // Load the view's content, with the params passed
    $view_content = $this->CI->load->view($view_name, $params, TRUE);

    // Now load the layout, and pass the view we just rendered
    $this->CI->load->view('laytous/' . $layout, array(
      'content_for_layout' => $view_content,
      'title_for_layout' => $separated_title_for_layout
    ));
  }

  public function add_include($path, $prepend_base_url = TRUE)
  {
    if ($prepend_base_url)
    {
      $this->CI->load->helper('url'); // Load this just to be sure
      $this->file_includes[] = base_url() . $path;
    }
    else
    {
      $this->file_includes[] = $path;
    }

    return $this; // This allows chain-methods
  }

  public function print_includes()
  {
    // Initialize a string that will hold all includes
    $final_includes = '';

    foreach ($this->includes as $include)
    {
      // Check if it's a JS or a CSS file
      if (preg_match('/js$/', $include))
      {
        // It's a JS file
        $final_includes .= '<script type="text/javascript" src="' . $include . '"></script>';
      }
      elseif (preg_match('/css$/', $include))
      {
        // It's a CSS file
        $final_includes .= '<link href="' . $include . '" rel="stylesheet" type="text/css" />';
      }

      return $final_includes;
    }
  }
}

最佳答案

美元是“字符串结尾”anchor 。仅当“js”位于字符串末尾时匹配才会成功。

关于php - preg_match语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9725460/

相关文章:

database - CI 中的多对多关系(不使用 ORM)

php - 更改根 SimpleXML 元素的文本

php - 不允许在 CodeIgniter 中回复邮件?

php - 来自 codeigniter 中输入插入的 NULL 变量值

php - 无法加载请求的语言文件 : language/en/form_validation_lang. php

php - 带有限制和偏移量的 CodeIgniter sql 查询不显示结果

php - 使用 MySQL 结果集在 PHP 中创建图像

php - 根据通过复选框获得的表在 SQL 中存储数据?

php - 将多个单维数组组合并转置为单个多维数组

php - Codeigniter 返回代码 0 db 错误而没有消息