scarhand Posted November 6, 2008 Share Posted November 6, 2008 im trying to get preg_match to get part of a string for me the strings are formatted like this: name_number i just want the name from the string so whatever is before the _ any help would be appreciated Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/ Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 preg_match("/(.+)_/", $file); doesnt work Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683791 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 neither does this $name = preg_match('([a-zA-Z0-9]+)_(.*)', $file, $matches); $name = $matches[1]; Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683797 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 got it $name = preg_match('/([a-zA-Z0-9]+)_(.*)/', $file, $matches); $name = $matches[1]; Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683804 Share on other sites More sharing options...
.josh Posted November 6, 2008 Share Posted November 6, 2008 This is shorter: $string = "name_number"; preg_match("/(.+)_/", $string, $result); echo $result[1]; But I'm not a regex expert. Could be something better. The reason why that didn't work for you in your earlier post was because you forgot the 3rd preg_match argument (assigning result somewhere). Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683807 Share on other sites More sharing options...
effigy Posted November 6, 2008 Share Posted November 6, 2008 Use explode for this level of simplicity. Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683817 Share on other sites More sharing options...
.josh Posted November 6, 2008 Share Posted November 6, 2008 Use explode for this level of simplicity. I was going to initially suggest that, but thought maybe he had some specific reason he wanted to preg_match it. $string = "name_number"; $result = explode('_', $string); echo $result[0]; Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683819 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 thanks guys actualy, after name_ there could be second or even third _ symbols which is why i am sticking with my solution Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683833 Share on other sites More sharing options...
.josh Posted November 6, 2008 Share Posted November 6, 2008 Well that's not what you said in the OP... Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683835 Share on other sites More sharing options...
scarhand Posted November 6, 2008 Author Share Posted November 6, 2008 Well that's not what you said in the OP... my bad, thanks for the help though Link to comment https://forums.phpfreaks.com/topic/131648-solved-simple-preg_match-help/#findComment-683956 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.