Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
modifier fix_http

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 537

PostPosted: Tue Sep 18, 2012 1:32 pm    Post subject: modifier fix_http Reply with quote

Adds http: if necessary for URLs. Works for smarty2, might need to be changed for smarty3, I don't know.


/**
* Smarty fix_http modifier plugin
*
* Type: modifier<br>
* Name: fix_http<br>
* Purpose: add http:// to beginning of link if necessary
* used as smarty filter
*
* @author Doug Davis
* @param $url string
*/

function smarty_modifier_fix_http($url)
{
$url = trim($url);

if ($url && strncasecmp($url,'http:', 5) != 0 && strncasecmp($url,'https:', 6) != 0) {
$url = 'http://'.$url;
}

return $url;
}
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 698
Location: Germany, border to Switzerland

PostPosted: Tue Sep 18, 2012 2:32 pm    Post subject: Reply with quote

I'd probably prefer something along the lines of:

Code:
function smarty_modifier_scheme($string)
  if (!preg_match('#^[a-zA-Z0-9-]://#', $string)) {
    return '//' . $string;
  }

  return $string;
}


Note that "//google.com" is a valid URL. it makes the browser decide if HTTP or HTTPS should be used depending on the scheme used for the current document.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 537

PostPosted: Tue Sep 18, 2012 2:33 pm    Post subject: Reply with quote

rodneyrehm wrote:
I'd probably prefer something along the lines of:

Code:
function smarty_modifier_scheme($string)
  if (!preg_match('#^[a-zA-Z0-9-]://#', $string)) {
    return '//' . $string;
  }

  return $string;
}


Note that "//google.com" is a valid URL. it makes the browser decide if HTTP or HTTPS should be used depending on the scheme used for the current document.



That's useful to know. Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP