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; ?>
Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Rory Web Rothon
  • 143
  • 1
  • 2
  • 5
  • 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 '11 at 14:49
  • http://stackoverflow.com/questions/4487794/checking-if-string-contains-http – Sudhir Bastakoti Dec 21 '11 at 14:50

3 Answers3

79

parse_url() can help...

$parsed = parse_url($urlStr);
if (empty($parsed['scheme'])) {
    $urlStr = 'http://' . ltrim($urlStr, '/');
}
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • 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 . – Rory Web Rothon Dec 21 '11 at 14:55
  • http://stackoverflow.com/a/8591679/1110064 Figured it out and it works, i just dont understand the 'sceme' part. what is this lol – Rory Web Rothon Dec 21 '11 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" http://php.net/manual/en/function.parse-url.php – Dutchie432 Dec 21 '11 at 16:17
  • 2
    This is a good solution, because it will not fail on case-sensitivity, or with `https://` +1 – Dutchie432 Dec 21 '11 at 16:20
  • What is the ltrim() bit for? – Dale C. Anderson Jun 03 '14 at 23:17
  • @DaleAnderson Strips off extra / at end of a link was `http://google.com/` for example would be adjusted to `http://google.com`. Not necessary but a nice touch. I suppose it would also strip any / before the url(which would be necessary) but seems an unlikely scenario. – Jeff Aug 04 '17 at 14:04
  • @Jeff This is left trim, not right trim. This part fixes the URL if you provide it like `//somesite.com/` so it does not become `https:////somesite.com/` – Дамян Станчев Aug 17 '20 at 13:57
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.

Tyil
  • 1,797
  • 9
  • 14
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.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • Using string functions makes it harder than necessary to deal with case and `https://` – DaveRandom Dec 21 '11 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 '11 at 14:51