basher400 Posted April 9, 2015 Share Posted April 9, 2015 hiI have strings that contain the url of an yahoo group and it may appear in any of the following patterns: it.groups.yahoo.com/group/batmaras/links/it.groups.yahoo.com/group/batmaras/linksit.groups.yahoo.com/group/batmaras/it.groups.yahoo.com/group/batmaras I need a regex that can always capture the "batmaras" (I can't guess that part, I need to extract it using regex) and give me a result as follows: it.groups.yahoo.com/group/batmaras I want to remove anything after the "batmaras" part.this is what I tried and is not working for me: (it\.groups\.yahoo\.com\/group\/.*?)\/? Quote Link to comment https://forums.phpfreaks.com/topic/295373-regex-for-getting-yahoo-groups-account/ Share on other sites More sharing options...
Solution Psycho Posted April 9, 2015 Solution Share Posted April 9, 2015 (edited) $urls = array( 'it.groups.yahoo.com/group/batmaras/links/', 'it.groups.yahoo.com/group/batmaras/links', 'it.groups.yahoo.com/group/batmaras/', 'it.groups.yahoo.com/group/batmaras' ); foreach($urls as $url) { echo "<br><b>INPUT:</b> {$url}<br>\n"; if(preg_match("#it\.groups\.yahoo.com/group/([^\/]*)#is", $url, $groupMatch)) { $groupName = $groupMatch[1]; echo "<b>GROUP NAME:</b> {$groupName}<br>\n"; $groupURL = "it.groups.yahoo.com/group/{$groupName}"; echo "<b>GROUP URL:</b> {$groupURL}<br>\n"; } else { //Did not match group name echo "<b>No Group Name found</b><br>\n"; } } Results INPUT: it.groups.yahoo.com/group/batmaras/links/ GROUP NAME: batmaras GROUP URL: it.groups.yahoo.com/group/batmaras INPUT: it.groups.yahoo.com/group/batmaras/links GROUP NAME: batmaras GROUP URL: it.groups.yahoo.com/group/batmaras INPUT: it.groups.yahoo.com/group/batmaras/ GROUP NAME: batmaras GROUP URL: it.groups.yahoo.com/group/batmaras INPUT: it.groups.yahoo.com/group/batmaras GROUP NAME: batmaras GROUP URL: it.groups.yahoo.com/group/batmaras Edited April 9, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/295373-regex-for-getting-yahoo-groups-account/#findComment-1508579 Share on other sites More sharing options...
basher400 Posted April 9, 2015 Author Share Posted April 9, 2015 thanks mate. Quote Link to comment https://forums.phpfreaks.com/topic/295373-regex-for-getting-yahoo-groups-account/#findComment-1508587 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.