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 echo "\n---\n"; }
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
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 => '', ) ---

preferences:
186.7 ms | 404 KiB | 176 Q