<?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;
preferences:
23.93 ms | 407 KiB | 5 Q