Shorten URL by certain string

I have an URL field for an Instagram profile. I want it to display @myprofile instead of https://www.instagram.com/myprofile. How can I achieve this?

Do you mean in the Panel or on the frontend?

I mean the frontend. Besides, I want it to open in a new window. How can I achieve this with an URL field?

The url field only returns a string, so if you create your link tag in the frontend, you can add the target="_blank" attribute to open the link in a new tab/window if you really want to.

echo '@' . Url::path('https://instagram.com/myprofile'); // myprofile

You could do something like this:

<a href="<?= $url = $page->instagram() ?>" target="_blank" rel="nofollow">
  <?= preg_replace('/.*\//', '@', $url) ?>
</a>

Edit: You probably also want to add the rel="nofollow" attribute.

1 Like

Thank you!