Check if URL has "http://" with a small UDT

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Locked
User avatar
manuel
Power Poster
Power Poster
Posts: 353
Joined: Fri Nov 30, 2007 9:15 am

Check if URL has "http://" with a small UDT

Post by manuel »

Issue:
- users are allowed to enter URL's in a text input box.
- That URL has to be used later on to create a href
- If the user didn't enter "http://", the href will be send visitors to "http://www.yourwebsite.com/www.someotherwebsite.com" instead of "http://someotherwebsite.com" (may be only the case if you use pretty URL's, didn't test...)

Solution:

Use http://stackoverflow.com/a/8591679/1338546 in a UDT
In the template:

Code: Select all

<a href="{checkurl url="{$oneuser.properties.website}"}">...</a>
The UDT named checkurl:

Code: Select all

$urlStr = $params['url'];
$parsed = parse_url($urlStr);
if (empty($parsed['scheme'])) {
    $urlStr = 'http://' . ltrim($urlStr, '/');
};
echo $urlStr;
Do you like your open source cms? Buy from the CMSMS partners || Donate
User avatar
manuel
Power Poster
Power Poster
Posts: 353
Joined: Fri Nov 30, 2007 9:15 am

Re: Check if URL has "http://" with a small UDT

Post by manuel »

ps: you can also save the result in a variable instead of using echo...

Replace the echo with the code below and a variable called "newurl" will be available in your template after the calling the fixurl UDT.

Code: Select all

$smarty = cmsms()->GetSmarty();
$smarty->assign('newurl', $urlStr);
Do you like your open source cms? Buy from the CMSMS partners || Donate
Locked

Return to “Tips and Tricks”