doujiu3768 2011-05-24 14:23
浏览 44
已采纳

如果http://在字符串中则保留,否则如果不添加它

I have a input that you enter a URL, i basically want to write some php that says if the domain containts "http://" then leave it be, else if not then add it to the beginning. This is what I have so far...

$domain = $_POST["domain"];

if (strpos($domain, "http://")) {
return $domain;
} else {
$domain = "http://" . $domain;
}

This doesnt seem to work..

it doesnt add the http:// on if it doesnt contain http://.

  • 写回答

8条回答 默认 最新

  • doujiang2641 2011-05-24 14:27
    关注

    "http://" then leave it be, else if not then add it to the beginning.

    How about adding adding it regardless? I find that to be easier:

    <?php
    $url = 'http://www.google.com';
    echo 'http://' . preg_replace( '~^http://~', '', $url );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错