I currently have a dynamic PHP page that displays values from a database to display a URL like this:
<a target="_blank" href="<?php echo($row['Virtual_Tour']); ?>"><?php echo($row['Virtual_Tour']); ?></a>
This works great EXCEPT when the data does not include a http://. I have not control over the data. How do I test to see if the echo starts with http:// and if not inserts that as well to guarantee the URL is properly formatted?
if(substr($row['Virtual_Tour'],0,7)=="http://")
{
//starts with http://, no formatting needed
}
else
{
$row['Virtual_Tour']="http://".$row['Virtual_Tour'];
}
If I get it correctly, if your url does not contain http://
you want to add it, since some answers provide solution to show it only like this:<a href="something.com">http://something.com</a>
.
If I am right, you could use this:
$string = "something.com";
$string = substr($string,0,6) != 'http://' ? "http://".$string : $string;
echo $string;
You can just remove the exist http://, if it exists, and put a newone like below code
<a target="_blank" href="<?php echo "http://".str_replace("http://","",$row['Virtual_Tour']); ?>"><?php echo "http://".str_replace("http://","",( $row['Virtual_Tour']); ?></a>
you can do this also with https:// like
<a target="_blank" href="<?php echo "http://".str_replace("https://","", str_replace("http://","",$row['Virtual_Tour'])); ?>"><?php echo "http://". str_replace("https://","",str_replace("http://","",( $row['Virtual_Tour'])); ?></a>