Link to home
Start Free TrialLog in
Avatar of EMB01
EMB01Flag for United States of America

asked on

Condition Fails to Return Expected Value

The attached code snippet returns prepends "http://www.example.com" to the $url variables that don't contain "http://." Then, the script appends a "<br />" tag. For $url variables that do contain "http://," the script simply echoes the $url variable but it still get's prepended with "http://www.example.com."

If the following text represents $url:
/webpage.html

I want the script to change it to:
http://www.example.com/webpage.html

If the following text represents $url:
http://www.example.com/webpage.html

I want the script to simply echo the same:
http://www.example.com/webpage.html

Ideally, I would also like to filter out "mailto:" and similar tags from being prepended with "http://."

Thank you.
<?php
// example...
// $url could be "/webpage.html" or "http://www.example.com/webpage.html" or even "mailto:webmaster@example.com."
// all of which should be returned accordingly
 
if (!strpos($url, "http://")) {
  echo "http://www.example.com".$url."<br />";
} else {
  echo $url."<br />";
}

Open in new window

Avatar of mostart
mostart

its because your string starts with http:// so strpos return 0

Try like this:

if (strpos($url, "http://") === false) {
  echo "http://www.example.com".$url."<br />";
} else {
  echo $url."<br />";
}

Open in new window

Avatar of EMB01

ASKER

Okay, that works. What about the "mailto:" part?
ASKER CERTIFIED SOLUTION
Avatar of mostart
mostart

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EMB01

ASKER

Thanks, again! Two in one day for me, mostart.