Find http:// oder https:// and return rest of string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Find http:// oder https:// and return rest of string

Post by visionmaster »

Hello together,

I would like to find http:// oder https:// in a string and if one of the two is matched, return rest of string.

Examples:
http://www.devnetwork.net => http://www.devnetwork.net
https://www.devnetwork.net => http://www.devnetwork.net

Finding the patterns ist not the problem. But how can I return the rest of the string, if there ist a match. I guess backreferences is the solution, but I'm a newbie in backreferences...

Code: Select all

if (preg_match('/(http|https):\/\//', $row->url)) {
						
}
else {

}


Thanks for your help!
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

$url = "https://www.devnetwork.net";
if (preg_match("/(http|https):\/\/(.*?)$/i", $url, $matches) > 0)
 echo $matches[2];
Post Reply