如何不用 substr 切断最后一个字符?
how to NOT cut off the last char with substr?
得到以下字符串:http://example.de/?param=1¶m2=http://example2.com/
我想得到 param2= 后面的所有内容。首先想到的是 substr,但它从来没有让我完全落后于 param2=。无论我如何使用它,它总是 returns http://example2.com (last char missing). I red the Documentation 为此,但仍然没有成功。
我已经试过了:
$url = 'http://example.de/?param=1¶m2=http://example2.com/';
$piece = substr($url, strpos($url, 'param2=') + 7 ,count($url)); //+7 cause of param2= should not be included
=> h
$piece = substr($url, strpos($url, 'param2=') + 7 , -count($url));
=> http://example2.com
$piece = substr($url, strpos($url, 'param2=') + 7 , +count($url));
=> h
$piece = substr($url, strpos($url, 'param2=') + 7 , -(count($url)+1));
=> http://example2.co
$piece = substr($url, strpos($url, 'param2=') + 7 , -(count($url)-1));
=> (empty)
$piece = substr($url, strpos($url, 'param2=') + 7 , -1);
=> http://example2.com
$piece = substr($url, strpos($url, 'param2=') + 7 , -0);
=> (empty)
所以我从来没有得到最后一个字符,也许你知道我做错了什么。
你可以使用explode()
:-
<?php
$string = 'http://example.de/?param=1¶m2=http://example2.com/';
echo explode('¶m2=',$string)[1];
对于您在评论中的查询,请按以下方式操作:-
得到以下字符串:http://example.de/?param=1¶m2=http://example2.com/ 我想得到 param2= 后面的所有内容。首先想到的是 substr,但它从来没有让我完全落后于 param2=。无论我如何使用它,它总是 returns http://example2.com (last char missing). I red the Documentation 为此,但仍然没有成功。
我已经试过了:
$url = 'http://example.de/?param=1¶m2=http://example2.com/';
$piece = substr($url, strpos($url, 'param2=') + 7 ,count($url)); //+7 cause of param2= should not be included
=> h
$piece = substr($url, strpos($url, 'param2=') + 7 , -count($url));
=> http://example2.com
$piece = substr($url, strpos($url, 'param2=') + 7 , +count($url));
=> h
$piece = substr($url, strpos($url, 'param2=') + 7 , -(count($url)+1));
=> http://example2.co
$piece = substr($url, strpos($url, 'param2=') + 7 , -(count($url)-1));
=> (empty)
$piece = substr($url, strpos($url, 'param2=') + 7 , -1);
=> http://example2.com
$piece = substr($url, strpos($url, 'param2=') + 7 , -0);
=> (empty)
所以我从来没有得到最后一个字符,也许你知道我做错了什么。
你可以使用explode()
:-
<?php
$string = 'http://example.de/?param=1¶m2=http://example2.com/';
echo explode('¶m2=',$string)[1];
对于您在评论中的查询,请按以下方式操作:-