htmanning Posted September 7, 2011 Share Posted September 7, 2011 Hi - newbie to php - I need to search a string and possibly replace part of it. This is how I would do it in perl: if ($homepage) { if ($homepage !~ /http/i) { $homepage = "http:\/\/" . $homepage; } } I have a homepage field in the database but sometimes people put the http in, and sometimes they leave it out. In perl I can figure it out and format accordingly but it doesn't work in PHP. I'm not sure if the first line even works in PHP. Maybe if to say something like: if $homepage is not equal to nothing. I'm not sure. Any help would be appreciated. Thanks. Link to comment https://forums.phpfreaks.com/topic/246593-replace-part-of-string/ Share on other sites More sharing options...
voip03 Posted September 7, 2011 Share Posted September 7, 2011 preg_replace http://www.php.net/manual/en/function.preg-replace.php Link to comment https://forums.phpfreaks.com/topic/246593-replace-part-of-string/#findComment-1266309 Share on other sites More sharing options...
QuickOldCar Posted September 7, 2011 Share Posted September 7, 2011 going by what you have there if ($homepage != "") { if (!preg_match("/http/i",$homepage)) { $homepage = "http://" . $homepage; } } Link to comment https://forums.phpfreaks.com/topic/246593-replace-part-of-string/#findComment-1266396 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.