Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting string to URL in PHP

I have a string in php and I want to convert it into a URL,Basically I am taking input from the user so I am not sure either he write http://www.google.com or just www.google.com or maybe just google.com, In either cases I have to show him the URL link in an anchor tag. I am looking for any PHP function that can do this, Otherwise I have to manually search for http and other If else condition will surely gonna waste my time,If there is no function I would be more than happy to write one and make it open to the community. EDIT: Another thing is the best way to write HTML in PHP, I mean is it good if we use echo' long HTML Forms' etc any solution Thanks


1 Answers

you can still use parse_url function

$parsed = parse_url($urlStr);
if (empty($parsed['scheme'])) {
    $urlStr = 'http://' . ltrim($urlStr, '/');
}

about writing html with PHP, it's okay to use echo function, just make sure you escape the double quotes with backslash

like image 117
nhenrique Avatar answered Jul 23 '26 22:07

nhenrique



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!