?
This document uses PHP Chinese website manual Release
URL幫助文件包含有助于使用URL的功能。
加載此助手
可用職能
使用以下代碼加載此助手:
$this->load->helper('url');
現(xiàn)有下列職能:
site_url([$uri = ''[, $protocol = NULL]])
參數(shù): | $ uri(string) - URI字符串$ protocol(string) - 協(xié)議,例如'http'或'https' |
---|---|
返回: | 網(wǎng)站網(wǎng)址 |
返回類型: | 串 |
$ uri(字符串) - URI字符串
$ protocol(string) - 協(xié)議,例如'http'或'https'
Returns: Site URL
Return type: string
按照配置文件中的指定返回您的站點URL。index.php文件(或者你在你的配置文件中設(shè)置為你的站點**索引\ _page **的文件)將被添加到URL中,你傳遞給函數(shù)的任何URI段以及** url \ _suffix **在你的配置文件中設(shè)置。
我們鼓勵您在需要生成本地URL時使用此函數(shù),以便在URL更改時頁面變得更可移植。
可以選擇將段作為字符串或數(shù)組傳遞給函數(shù)。下面是一個字符串示例:
echo site_url('news / local / 123');
上面的示例將返回如下內(nèi)容:http://example.com/index.php/news/local/123
下面是作為數(shù)組傳遞的段的示例:
$ segments = array('news','local','123'); echo site_url($ segments);
此函數(shù)是CI_Config::site_url()
有關(guān)更多信息,請參見配置庫文件。
base_url($uri = '', $protocol = NULL)
參數(shù): | $ uri(string) - URI字符串$ protocol(string) - 協(xié)議,例如'http'或'https' |
---|---|
返回: | 基本網(wǎng)址 |
返回類型: | 串 |
$ uri(字符串) - URI字符串
$ protocol(string) - 協(xié)議,例如'http'或'https'
Returns: Base URL
Return type: string
按照配置文件中的規(guī)定返回您的網(wǎng)站基礎(chǔ)URL。例:
echo base_url();
此函數(shù)返回與site_url()
,沒有指數(shù)[醫(yī)]頁或URL[醫(yī)]后綴被追加。
也像site_url()
,可以將段作為字符串或數(shù)組提供。下面是一個字符串示例:
echo base_url(“blog / post / 123”);
上面的示例將返回如下內(nèi)容:http://example.com/blog/post/123
這是有用的,因為不像site_url()
,您可以向文件(如圖像或樣式表)提供字符串。例如:
echo base_url(“images / icons / edit.png”);
這會給你這樣的東西:http://example.com/映像/圖標(biāo)/ed.png
此函數(shù)是CI_Config::base_url()
有關(guān)更多信息,請參見配置庫文件。
current_url()
返回: | 當(dāng)前的網(wǎng)址 |
---|---|
返回類型: | 串 |
uri_string()
返回: | 一個URI字符串 |
---|---|
返回類型: | 串 |
index_page()
返回: | 'index_page'值 |
---|---|
返回類型: | mixed |
anchor($uri = '', $title = '', $attributes = '')
參數(shù): | $ uri(字符串) - URI字符串$ title(字符串) - 錨定標(biāo)題$ attributes(mixed) - HTML屬性 |
---|---|
返回: | HTML超鏈接(錨標(biāo)簽) |
返回類型: | 串 |
$ uri(字符串) - URI字符串
$ title(字符串) - 錨定標(biāo)題
$ attributes(mixed) - HTML屬性
Returns: HTML hyperlink (anchor tag)
Return type: string
根據(jù)您的本地網(wǎng)站URL創(chuàng)建標(biāo)準(zhǔn)的HTML定位鏈接。
第一個參數(shù)可以包含您希望附加到URL的任何段。就像site_url()
函數(shù),段可以是字符串,也可以是數(shù)組。
注
如果您正在構(gòu)建應(yīng)用程序內(nèi)部的鏈接,請不要包含基本URL(http:// ...)。這將從您的配置文件中指定的信息自動添加。僅包含您希望附加到URL的URI段。
第二部分是你想要鏈接的文本。如果將其保留為空白,則將使用URL。
第三個參數(shù)可以包含要添加到鏈接中的屬性列表。屬性可以是簡單的字符串或關(guān)聯(lián)數(shù)組。
以下是一些例子:
echo anchor('news/local/123', 'My News', 'title="News title"'); // Prints: <a href="http://example.com/index.php/news/local/123" title="News title">My News</a> echo anchor('news/local/123', 'My News', array('title' => 'The best news!')); // Prints: <a href="http://example.com/index.php/news/local/123" title="The best news!">My News</a> echo anchor('', 'Click here'); // Prints: <a href="http://example.com">Click Here</a>
anchor_popup($uri = '', $title = '', $attributes = FALSE)
參數(shù): | $ uri(字符串) - URI字符串$ title(字符串) - 錨定標(biāo)題$ attributes(mixed) - HTML屬性 |
---|---|
返回: | 彈出超鏈接 |
返回類型: | 串 |
$ uri(字符串) - URI字符串
$ title(字符串) - 錨定標(biāo)題
$ attributes(mixed) - HTML屬性
Returns: Pop-up hyperlink
Return type: string
Nearly identical to the [`anchor()`](about:blank#anchor) function except that it opens the URL in a new window. You can specify JavaScript window attributes in the third parameter to control how the window is opened. If the third parameter is not set it will simply open a new window with your own browser settings.
下面是一個具有屬性的示例:
$atts = array( 'width' => 800, 'height' => 600, 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => 0, 'screeny' => 0, 'window_name' => '_blank' ); echo anchor_popup('news/local/123', 'Click Me!', $atts);
注
上述屬性是函數(shù)的默認(rèn)值,所以你只需要設(shè)置與你需要的不同。如果您希望函數(shù)使用其所有默認(rèn)值,只需在第三個參數(shù)中傳遞一個空數(shù)組:| | echo anchor_popup('news / local / 123','Click Me!',array());
注
該window_name是不是一個真正的屬性,但參數(shù)給JavaScript window.open() <http://www.w3schools.com/jsref/met_win_open.asp>
方法,它可以接受窗口名稱或窗目標(biāo)。
注
上面列出的任何其他屬性都將被解析為錨標(biāo)記的HTML屬性。
mailto($email, $title = '', $attributes = '')
參數(shù): | $ email(string) - 電子郵件地址$ title(字符串) - 定位標(biāo)題$ attributes(mixed) - HTML屬性 |
---|---|
返回: | “郵件到”超鏈接 |
返回類型: | 串 |
$ email(字符串) - 電子郵件地址
$ title(字符串) - 錨定標(biāo)題
$ attributes(mixed) - HTML屬性
Returns: A “mail to” hyperlink
Return type: string
創(chuàng)建一個標(biāo)準(zhǔn)的HTML電子郵件鏈接。用法示例:
echo mailto('email protected','點擊這里與我聯(lián)系');
就像anchor()
選項卡上,您可以使用第三個參數(shù)設(shè)置屬性:
$ attributes = array('title'=>'Mail me'); echo mailto('email protected','聯(lián)系我',$ attributes);
safe_mailto($email, $title = '', $attributes = '')
參數(shù): | $ email(string) - 電子郵件地址$ title(字符串) - 定位標(biāo)題$ attributes(mixed) - HTML屬性 |
---|---|
返回: | 垃圾郵件安全的“郵件至”超鏈接 |
返回類型: | 串 |
$ email(字符串) - 電子郵件地址
$ title(字符串) - 錨定標(biāo)題
$ attributes(mixed) - HTML屬性
Returns: A spam-safe “mail to” hyperlink
Return type: string
Identical to the [`mailto()`](about:blank#mailto) function except it writes an obfuscated version of the _mailto_ tag using ordinal numbers written with JavaScript to help prevent the e-mail address from being harvested by spam bots.
auto_link($str, $type = 'both', $popup = FALSE)
參數(shù): | $ str(string) - 輸入字符串$ type(string) - 鏈接類型('email','url'或'both')$ popup(bool) - 是否創(chuàng)建彈出鏈接 |
---|---|
返回: | 鏈接的字符串 |
返回類型: | 串 |
$ str(string) - 輸入字符串
$ type(string) - 鏈接類型('email','url'或'both')
$ popup(bool) - 是否創(chuàng)建彈出鏈接
Returns: Linkified string
Return type: string
自動將包含在字符串中的URL和電子郵件地址轉(zhuǎn)換為鏈接。例:
$ string = auto_link($ string);
第二個參數(shù)決定URL和電子郵件是否被轉(zhuǎn)換,或者僅僅是其中一個.。如果未指定參數(shù),則為默認(rèn)行為。電子郵件鏈接被編碼為safe_mailto()
如上文所示。
僅轉(zhuǎn)換URL:
$ string = auto_link($ string,'url');
只轉(zhuǎn)換電子郵件地址:
$ string = auto_link($ string,'email');
第三個參數(shù)確定鏈接是否顯示在新窗口中。該值可以是TRUE或FALSE(布爾值):
$ string = auto_link($ string,'both',TRUE);
url_title($str, $separator = '-', $lowercase = FALSE)
參數(shù): | $ str(string) - 輸入字符串$ separator(string) - 字符分隔符$ lowercase(bool) - 是否將輸出字符串轉(zhuǎn)換為小寫 |
---|---|
返回: | URL格式的字符串 |
返回類型: | 串 |
$ str(string) - 輸入字符串
$ separator(string) - 單詞分隔符
$ lowercase(bool) - 是否將輸出字符串轉(zhuǎn)換為小寫
Returns: URL-formatted string
Return type: string
Takes a string as input and creates a human-friendly URL string. This is useful if, for example, you have a blog in which you’d like to use the title of your entries in the URL. Example:
$ title =“CSS有什么問題?”; $ url_title = url_title($ title); //產(chǎn)生:CSS錯誤
第二個參數(shù)確定單詞分隔符。默認(rèn)使用破折號。首選選項是:-(短劃線)或_(下劃線)
例子:
$ title =“CSS有什么問題?”; $ url_title = url_title($ title,'underscore'); //產(chǎn)生:Whats_wrong_with_CSS
注
舊用法‘破折號’和‘下劃線’作為第二個參數(shù)被廢棄。
第三個參數(shù)確定是否強制使用小寫字符。默認(rèn)情況下他們不是。選項是布爾值TRUE / FALSE。
例子:
$ title =“CSS有什么問題?”; $ url_title = url_title($ title,'underscore',TRUE); //產(chǎn)生:whats_wrong_with_css
prep_url($str = '')
參數(shù): | $ str(string) - URL字符串 |
---|---|
返回: | 協(xié)議前綴的URL字符串 |
返回類型: | 串 |
$ str(string) - URL字符串返回:協(xié)議加前綴的URL字符串返回類型:字符串在URL中缺少協(xié)議前綴的情況下,此函數(shù)將添加http://。將URL字符串傳遞給像這樣的函數(shù):$ url = prep_url('example.com'); redirect($uri = '', $method = 'auto', $code = NULL)
參數(shù):$ uri(字符串) - URI字符串$方法(字符串) - 重定向方法('auto','location'或'refresh')$ code(字符串) - HTTP響應(yīng)代碼(通常為302或303)返回類型:void
$ uri(字符串) - URI字符串
$ method(string) - 重定向方法('auto','location'或'refresh')
$ code(字符串) - HTTP響應(yīng)代碼(通常為302或303)
Return type: void
“標(biāo)題重定向到”指定的URI。如果指定將構(gòu)建鏈接的完整網(wǎng)站URL,但對于本地鏈接,只需將URI段提供給要指向的控制器即可創(chuàng)建鏈接。該函數(shù)將根據(jù)您的配置文件值構(gòu)建URL。
可選的第二個參數(shù)允許您強制使用特定的重定向方法??捎玫姆椒ㄊ?strong>自動,位置和刷新,在IIS服務(wù)器上位置更快但可靠性更低。缺省值為auto,它將嘗試基于服務(wù)器環(huán)境智能地選擇方法。
可選的第三個參數(shù)允許您發(fā)送特定的HTTP響應(yīng)代碼--例如,這可以用于創(chuàng)建301重定向以用于搜索引擎。默認(rèn)響應(yīng)代碼為302。第三個參數(shù)是只可與位置重定向,而不是刷新.例子:
if($ logged_in == FALSE){redirect('/ login / form /'); } // 301重定向重定向('/ article / 13','location',301);
注
為了使此功能起作用,必須在將任何內(nèi)容輸出到瀏覽器之前使用它,因為它使用服務(wù)器標(biāo)題。
注
對于對頭的非常細(xì)粒度的控制,您應(yīng)該使用輸出庫set_header()
方法。
注
對于IIS用戶:如果您隱藏Server
http標(biāo)頭,汽車方法不會檢測到IIS,在這種情況下,建議您顯式使用刷新方法。
注
當(dāng)位置方法時,HTTPStatus代碼為303自動當(dāng)前通過POST訪問頁面并使用HTTP/1.1時,請選擇。