Regular Expressions - check to make sure string...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
superwormy
Forum Commoner
Posts: 67
Joined: Fri Oct 04, 2002 9:25 am
Location: CT

Regular Expressions - check to make sure string...

Post 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?
MeOnTheW3
Forum Commoner
Posts: 48
Joined: Wed Nov 13, 2002 3:28 pm
Location: Calgary, AB

Post 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

?>
Post Reply