Page 1 of 1

Regular Expressions - check to make sure string...

Posted: Thu Nov 14, 2002 1:11 pm
by superwormy
I want to check a string, and replace certain characters ONLY IF STRING DOES NOT have ' http:// ' in it.

How can I do this?

Posted: Thu Nov 14, 2002 2:06 pm
by MeOnTheW3
try this:

Code: Select all

<?php
$var_1 = "http://www.hello.com";
$var_2 = "www.hello.com";

function someName($var, $noString, $newChars, $oldChars)
{
   if(!strstr($var,$noString))
   {
      $var = str_replace($newChars,$oldChars,$var);
   }
   return $var;
}

// Then try it:
$var_1 = someName($var_1,'http://','.com','.net');  // still = http://www.hello.com
$var_2 = someName($var_2,'http://','.com','.net');  // now = http://www.hello.net

?>