Search code examples
phpsubstr

how to NOT cut off the last char with substr?


Got following String: http://example.de/?param=1&param2=http://example2.com/ I want to get everything behind param2=. First thought was substr, but it never gives me all behind param2=. No matter how I use it, it always returns http://example2.com (last char missing). I red the Documentation for this, but still no success with this.

I already tried:

$url = 'http://example.de/?param=1&param2=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)

So I never get the last char, maybe you know what I am doing wrong.


Solution

  • You can use explode():-

    <?php
    
    $string = 'http://example.de/?param=1&param2=http://example2.com/';
    
    echo explode('&param2=',$string)[1];
    

    Output:-https://eval.in/657097

    For your query in comment, do like below:-

    https://eval.in/657101