0

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?

3 Answers 3

1
if(substr($row['Virtual_Tour'],0,7)=="http://")
{
  //starts with http://, no formatting needed
}
else 
{
  $row['Virtual_Tour']="http://".$row['Virtual_Tour'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

0

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;

working example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.