phpurlquery-stringtext-extractionurl-parsing

Get the leading part of a URL string before the first ampersand


I need to parse the following string:

$str = 'http://test.al/admin/?plugin=pages&act=delete&file=test-page';

It's dynamic and could contain more &-symbols.

I want to get everything but the part starting from "&..." so the result should be:

http://test.al/admin/?plugin=pages

Solution

  • Use strtok():

    <?php
    $str = 'http://test.al/admin/?plugin=pages&act=delete&file=test-page';
    $result = strtok($str, '&');
    
    var_dump($result); // outputs "http://test.al/admin/?plugin=pages"