3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getBaseURLPath() { return 'https://example.com/sub-directory/'; } function getBaseURLPathOnly() { return '/'; } function buildWithBaseURLPathOld($info, $baseURLPath) { $result = ''; if (!empty($baseURLPath)) { $result = $baseURLPath; if (!empty($info)) { $path = explode('/', $info); // Remove base path from the path info. $extractedInfo = array_pop($path); if (!empty($extractedInfo)) { $result .= $extractedInfo; } } } return $result; } function buildWithBaseURLPathNew($info, $baseURLPath) { $result = ''; if (!empty($baseURLPath)) { $result = $baseURLPath; if (!empty($info)) { // Remove base path from the path info. $extractedInfo = str_replace($baseURLPath, '', $info); // Remove starting and ending slash. $extractedInfo = trim($extractedInfo, '/'); if (!empty($extractedInfo)) { $result .= $extractedInfo; } } } return $result; } echo "Old: " . buildWithBaseURLPathOld('saml/acs',getBaseURLPath()) . PHP_EOL; echo "New: " . buildWithBaseURLPathNew('saml/acs',getBaseURLPath()) . PHP_EOL; echo "Old: " . buildWithBaseURLPathOld('/saml/acs',getBaseURLPath()) . PHP_EOL; echo "New: " . buildWithBaseURLPathNew('/saml/acs',getBaseURLPath()) . PHP_EOL; echo "Old: " . buildWithBaseURLPathOld('/',getBaseURLPath()) . PHP_EOL; echo "New: " . buildWithBaseURLPathNew('/',getBaseURLPath()) . PHP_EOL; echo "Old: " . buildWithBaseURLPathOld('',getBaseURLPath()) . PHP_EOL; echo "New: " . buildWithBaseURLPathNew('',getBaseURLPath()) . PHP_EOL; // Case not considered where URL is not a URL... echo "Old: " . buildWithBaseURLPathOld('saml/acs',getBaseURLPathOnly()) . PHP_EOL; echo "New: " . buildWithBaseURLPathNew('saml/acs',getBaseURLPathOnly()) . PHP_EOL; echo "Old: " . buildWithBaseURLPathOld('/saml/acs',getBaseURLPathOnly()) . PHP_EOL; echo "New: " . buildWithBaseURLPathNew('/saml/acs',getBaseURLPathOnly()) . PHP_EOL; echo "Old: " . buildWithBaseURLPathOld('/',getBaseURLPathOnly()) . PHP_EOL; echo "New: " . buildWithBaseURLPathNew('/',getBaseURLPathOnly()) . PHP_EOL; echo "Old: " . buildWithBaseURLPathOld('',getBaseURLPathOnly()) . PHP_EOL; echo "New: " . buildWithBaseURLPathNew('',getBaseURLPathOnly()) . PHP_EOL;
Output for 8.1.0 - 8.1.30, 8.2.0 - 8.2.24, 8.3.0 - 8.3.12
Old: https://example.com/sub-directory/acs New: https://example.com/sub-directory/saml/acs Old: https://example.com/sub-directory/acs New: https://example.com/sub-directory/saml/acs Old: https://example.com/sub-directory/ New: https://example.com/sub-directory/ Old: https://example.com/sub-directory/ New: https://example.com/sub-directory/ Old: /acs New: /samlacs Old: /acs New: /samlacs Old: / New: / Old: / New: /

preferences:
48.71 ms | 407 KiB | 5 Q