3v4l.org

run code in 300+ PHP versions simultaneously
<?php $urls = [ 'www.example.com/myurl.html?unwantedthngs#hastag', 'www.example.com/myurl.html' ]; foreach ($urls as $url) { var_export(['strtok: ', strtok($url, '?')]); echo "\n"; var_export(['strstr/true: ', strstr($url, '?', true)]); // not reliable echo "\n"; var_export(['explode/2: ', explode('?', $url, 2)[0]]); // limit alls func to stop searching after first encounter echo "\n"; var_export(['substr/strrpos: ', substr($url, 0, strrpos( $url, "?"))]); // not reliable; still not with strpos() echo "\n---\n"; }
based on AqlIY
Output for 7.2.0 - 7.2.31, 7.3.0 - 7.3.19, 7.4.0 - 7.4.7, 8.0.0alpha1 - alpha2
array ( 0 => 'strtok: ', 1 => 'www.example.com/myurl.html', ) array ( 0 => 'strstr/true: ', 1 => 'www.example.com/myurl.html', ) array ( 0 => 'explode/2: ', 1 => 'www.example.com/myurl.html', ) array ( 0 => 'substr/strrpos: ', 1 => 'www.example.com/myurl.html', ) --- array ( 0 => 'strtok: ', 1 => 'www.example.com/myurl.html', ) array ( 0 => 'strstr/true: ', 1 => false, ) array ( 0 => 'explode/2: ', 1 => 'www.example.com/myurl.html', ) array ( 0 => 'substr/strrpos: ', 1 => '', ) ---