projectcleverweb / php-uri
一个用于处理URI的PHP库,该库基于URI标准设计。需要PHP 5.4或更高版本。此库替代并扩展了PHP的所有parse_url()功能,甚至还有一些实用的别名。
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-24 02:37:08 UTC
README
一个用于处理URI的PHP库,该库基于URI标准设计。需要PHP 5.4
或更高版本。此库替代并扩展了PHP的所有parse_url()
功能,甚至还有一些实用的别名。
版权(c)2014 Nicholas Jordon - 版权所有。
安装库
Composer
将以下内容添加到您的composer.json文件中
"require": { "projectcleverweb/php-uri":"~1.0" }
手动
只需将uri.lib.php
文件包含在您的应用程序的某个位置。
示例
示例 #1: 字符串操作
$uri = new uri('http://example.com/path/to/file.ext'); $uri->replace('QUERY', 'number=3'); $uri->replace('PATH', '/foo/bar'); $uri->append('PATH', '.baz'); $new = $uri->prepend('HOST', 'www.'); $uri->reset(); $original = $uri->str(); $uri->replace('FRAGMENT', 'Checkout'); $secure = $uri->replace('SCHEME_NAME', 'https'); echo $new.PHP_EOL; echo $original.PHP_EOL; echo $secure;
输出
http://www.example.com/foo/bar.baz?number=3 http://example.com/path/to/file.ext https://example.com/path/to/file.ext#Checkout
示例 #2: 连锁操作
需要更改很多,同时保持任何额外内容不变?将其链起来。
$uri1 = new uri('ftp://jdoe:pass1234@my-server.com/public_html'); // Lets upgrade to an admin account under sftp, but stay in the current directory. $uri1->chain()-> prepend('SCHEME_NAME', 's')-> replace('PORT', '22')-> replace('USER', 'admin')-> replace('PASS', 'secure-pass-123'); // NOTE: chain() methods always return the chain object, even if a method fails. echo $uri1; // Any failure results in the chain() error count geting incremented. if (0 < $uri->chain()->error_count) { print_f('The chain failed %1$s times!', $uri->chain()->error_count); }
输出
sftp://admin:secure-pass-123@my-server.com:22/public_html
示例 #3: 信息收集
$uri = new uri('http://example.com/path/to/file.ext?q=1'); if ($uri->scheme_name == 'https') { echo 'Uses SSL'.PHP_EOL; } else { echo 'Does not use SSL'.PHP_EOL; } // Change to an absolute path $abs_path = $_SERVER['DOCUMENT_ROOT'].$uri->path; echo $abs_path.PHP_EOL; // easier to read links printf('<a href="%1$s">%2$s</a>', $uri->str(), $uri->host.$uri->path); // FTP logins $uri = new uri('ftp://jdoe@example.com/my/home/dir'); $login = array( 'username' => $uri->user, 'password' => $user_input, 'domain' => $uri->host, 'path' => $uri->path );
输出
Does not use SSL /var/www/path/to/file.ext <a href="http://example.com/path/to/file.ext?q=1">example.com/path/to/file.ext</a>
示例 #4: 与广泛的URI兼容
与电子邮件、skype和ssh URI完美兼容。解析器直接基于URI标准,因此它将很好地处理不常见和新的URI类型。
$uri1 = new uri('git@github.com:ProjectCleverWeb/PHP-URI.git'); $uri2 = new uri('example@gmail.com'); // Publish you source to multiple services? echo $uri1.PHP_EOL; // PHP will automatically get the current URI echo $uri1->replace('HOST', 'gitlab.com').PHP_EOL; echo $uri1->replace('HOST', 'bitbucket.org').PHP_EOL.PHP_EOL; // Quick and easy email template URI $uri2->chain() ->replace('SCHEME', 'mailto:') ->query_replace('subject', 'Re: [Suggestion Box]') ->query_replace('body', 'More snickers in the break room please!') ; printf('<a href="%1$s">%2$s</a>', $uri2, $uri2->authority);
输出
git@github.com:ProjectCleverWeb/PHP-URI.git git@gitlab.com:ProjectCleverWeb/PHP-URI.git git@bitbucket.org:ProjectCleverWeb/PHP-URI.git <a href="mailto:example@gmail.com?subject=Re%3A%20%5BSuggestion%20Box%5D&body=More%20snickers%20in%20the%20break%20room%20please%21">example@gmail.com</a>
已知问题
- 克隆操作不符合预期(请使用
$clone = new \uri($original->str()); $clone->input = $original->input;
代替) - 您不能直接更改URI的权限。这是故意的,因为权限是从当前URI生成的。
许可证
MIT许可证(MIT)
版权(c)2014 Nicholas Jordon - 版权所有
特此授予任何获得此软件及其相关文档副本(“软件”)的人免费使用该软件的权利,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许向提供软件的人使用软件的权利,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的副本或实质性部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者都不应对任何索赔、损害或其他责任负责,无论这些责任是基于合同、侵权或其他原因,以及这些责任是否源于、因之而产生或与此软件或其使用或其他交易有关。