Ror*_*hon 10 php url replace if-statement http
我目前正在使用自定义字段输出编辑wordpress主题.我已成功完成所有编辑,一切正常.我的问题是,如果一个url被提交到自定义字段中,那么回声正好在那里,所以如果有人进入www.somesite.com,回声就是这样,并将其添加到域的末尾:www.mysite .com www.somesite.com.我想检查提供的链接是否http://
在开头有前缀,如果它有两个,但如果没有http://
在url之前回显.
我希望我尽可能地解释了我的问题.
$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; ?>
Run Code Online (Sandbox Code Playgroud)
Dav*_*dom 61
parse_url()
可以帮助...
$parsed = parse_url($urlStr);
if (empty($parsed['scheme'])) {
$urlStr = 'http://' . ltrim($urlStr, '/');
}
Run Code Online (Sandbox Code Playgroud)
您可以http://
使用strpos()检查是否在字符串的开头.
$var = 'www.somesite.com';
if(strpos($var, 'http://') !== 0) {
return 'http://' . $var;
} else {
return $var;
}
Run Code Online (Sandbox Code Playgroud)
这样,如果它http://
在var的最开头没有,它将http://
在它前面返回.否则它只会返回$var
自己.
归档时间: |
|
查看次数: |
34778 次 |
最近记录: |