यहां, हमने एक फ़ंक्शन सेट किया है जो "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