14

I am currently editing a wordpress theme with custom field outputs. I have successfully made all the edits and everything works as it should. My problem is that if a url is submitted into the custom field, the echo is exactly what was in there, so if someone enters www.somesite.com the echo is just that and adds it to the end of the domain: www.mysite.com www.somesite.com . I want to check to see if the supplied link has the http:// prefix at the beginning, if it has then do bothing, but if not echo out http:// before the url.

I hope i have explained my problem as good as i can.

$custom = get_post_meta($post->ID, 'custom_field', true);

<?php if ( get_post_meta($post->ID, 'custom_field', true) ) : ?>
    <a href="<?php echo $custom ?>"> <img src="<?php echo bloginfo('template_url');?>/lib/images/social/image.png"/></a>
    <?php endif; ?>
2
  • 1
    Well, you seem to know how to use classes and templates. You should be able to figure this out with substr even if you can't figure out how to do this with RegEx, strncmp, or any of the many other methods....
    – Dutchie432
    Dec 21, 2011 at 14:49

3 Answers 3

79

parse_url() can help...

$parsed = parse_url($urlStr);
if (empty($parsed['scheme'])) {
    $urlStr = 'http://' . ltrim($urlStr, '/');
}
7
  • lol thanks for the replies, but could somebody possibly show me an example of the required code displayed into my example? i am a total php noob and am slowly getting familiar but still hit endless speed humps . Dec 21, 2011 at 14:55
  • stackoverflow.com/a/8591679/1110064 Figured it out and it works, i just dont understand the 'sceme' part. what is this lol Dec 21, 2011 at 15:17
  • 4
    Did you maybe google parse_url to see what scheme is? parse_url returns an array of key/value items. See "Return Values" php.net/manual/en/function.parse-url.php
    – Dutchie432
    Dec 21, 2011 at 16:17
  • 2
    This is a good solution, because it will not fail on case-sensitivity, or with https:// +1
    – Dutchie432
    Dec 21, 2011 at 16:20
  • What is the ltrim() bit for? Jun 3, 2014 at 23:17
8

You can check if http:// is at the beginning of the string using strpos().

$var = 'www.somesite.com';

if(strpos($var, 'http://') !== 0) {
  return 'http://' . $var;
} else {
  return $var;
}

This way, if it does not have http:// at the very beginning of the var, it will return http:// in front of it. Otherwise it will just return the $var itself.

2
  • 5
    this will fail if the case does not match, or if the url starts with https://
    – Dutchie432
    Dec 21, 2011 at 16:19
  • In that case, one could use logic like if(!preg_match('#^https?://#', $var)){ Jul 26, 2017 at 17:16
2
echo (strncasecmp('http://', $url, 7) && strncasecmp('https://', $url, 8) ? 'http://' : '') . $url;

Remember, that strncmp() returns 0, when the first n letters are equal, which evaluates to false here. That may be a little bit confusing.

2
  • Using string functions makes it harder than necessary to deal with case and https://
    – DaveRandom
    Dec 21, 2011 at 14:50
  • 1
    See my edit. You can assume, that this will be the only two schemes, that may occur. Upvoted your answer anyway, because its obviously the cleaner one ;) It doesn't make sense, if I repeat it, thus I'll leave my one as an alternative, that may cover most cases, but "not that clean". Now also the case is irrelevant
    – KingCrunch
    Dec 21, 2011 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.