Forward slash added to Value returned by $modx->getOption in Snippet

I’m using Client Config to store a File path for an option named img_placeholder.

The database shows the expected value: skin/placeholder.jpg

Also as expected, calling $modx->getOption in my snippet prefixes the value returned with the media source basePath: assets/media/

The value expected from $modx->getOption is: assets/media/skin/placeholder.jpg

But the value I get has an extra forward slash: /assets/media/skin/placeholder.jpg

Can anyone suggest why this is?

The line in my snippet is below, specifically it’s the fallback to getOption that is setting the value for $img_src.

$img_src = $modx->getOption('src', $scriptProperties, $modx->getOption('img_placeholder'));

So it should just return assets/media/skin/placeholder.jpg?

Thanks

I believe the slash is the base URL ([[++base_url]]) that gets added to the path.

Understood, thanks.

Would adding something like this to the snippet be an acceptable solution?

if (strpos($img, '/') === 0) {
    $img = substr($img, 1);
}

Updated from original, autocorrect was on.

Or is there an existing MODX function that would be better suited?

If it helps to add context, the $img path value is passed to phpThumbOf. The reason I need to strip the / is because it makes phpThumbOf treat the path as absolute which throws an open_basedir restriction in place error.

Thanks

Sure.

You could also use the PHP function ltrimltrim($img, "/"), but it doesn’t really matter.

However one version is eminently more readable.

Thanks again

1 Like