Page 1 of 1

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

Posted: Fri May 02, 2014 7:24 pm
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;

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

Posted: Fri May 02, 2014 7:31 pm
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);