php - 使用php获取字符串的一部分

标签 php regex string preg-replace preg-match

如何使用PHP获取字符串的一部分?

我有一个这样的字符串。

$str = 'href="http://www.idontknow.com/areyousure?answer=yes"';

我只想要链接......像这样

$str_new = "http://www.idontknow.com/areyousure?answer=yes";

最佳答案

$str_new = substr($str, 6, -1);

substr()

If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).

If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, false will be returned.

If length is given and is 0, FALSE or NULL an empty string will be returned.

If length is omitted, the substring starting from start until the end of the string will be returned.

关于php - 使用php获取字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6814930/

相关文章:

php - Split() 已弃用

php - 有没有更有效的方法在 mysql 中实现同样的事情(通过 php)

JavaScript 正则表达式删除大写字母,但保留全大写单词?

java - Android 中的离线数据库

c# - 为什么 List<string>.Sort() 很慢?

带附件的 php mail() 返回 noname.txt

javascript - PHP - 使用 Javascript 和 PHP 上传多个文件

javascript - 为什么输入模式属性不适用于数字?

c# - 我的正则表达式做错了什么?

c# - 如何将带小数点的字符串解析为 double ?