My goal:
To capture the last part of a URL, whether or not a trailing slash exists, if the trailing slash is not part of a string at a URL similar to the following:
http:
My problem:
Each way I'm trying to do only allows a trailing slash, not a URL without a trailing slash, or does a trailing slash in the string that I want.
What have i tried?
1. I tried to add a slash:
$regex = "/.*?foo\.com\/p\/(.*)\//"; if ($c=preg_match_all ($regex, $url, $matches)) { $id=$matches[1][0]; print "ID: $id \n"; }
This results in an error if I don't have a trailing slash.
2. I tried to add a question mark:
$regex = "/.*?foo\.com\/p\/(.*)[\/]?/";
This leads to a slash, if it exists, inside my line.
My question is / tl; dr:
How can I create RegEx so as not to require a slash, but keep the slash from the previous line?
source share