如果URL PHP中不存在http://,如何添加?
在这里,我们设置了一个函数,该函数将“http://添加到字符串。假设我们传递了以下值-
example.com
我们想要的输出是“http://”,即实际链接-
http://example.com
为此,可以将点(。)表示法和条件匹配与preg_match()一起使用。
示例
<!DOCTYPE html>
<body>
<?php
function addingTheHTTPValue($stringValue) {
if (!preg_match("~^(?:f|ht)tps?://~i", $stringValue)) {
$stringValue = "http://" . $stringValue;
}
return $stringValue;
}
echo addingTheHTTPValue("example.com");
echo "<br>";
echo addingTheHTTPValue("https://example.com");
?>
</body>
</html>输出结果
http://example.com https://example.com
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短