physaux Posted November 2, 2009 Share Posted November 2, 2009 Does anyone see anything wrong with this: preg_match('/animal\/([^<]+)">SALLY/', $contents, $match); echo $match; The string that i would be reading would be like this: ef="/animal/cdj87sd3">SALLY</a> Where the begining is animal/ and the end is ">SALLY and the characters in the middle are lowercase letters and numbers, limit of 8 characters I want the variable $match to have those 8 random characters. could you please help me! Ty!! Quote Link to comment https://forums.phpfreaks.com/topic/180008-solved-help-me-with-my-preg_match-plz/ Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 Try this... preg_match('~animal/([a-z0-9]+){1,8}">SALLY~', $contents, $match); echo $match[1]; Note the 1,8 part means to make that section between 1 and 8 characters, if theres a more specific requirement just change those numbers. Quote Link to comment https://forums.phpfreaks.com/topic/180008-solved-help-me-with-my-preg_match-plz/#findComment-949670 Share on other sites More sharing options...
physaux Posted November 2, 2009 Author Share Posted November 2, 2009 Ok thanks, but I am just wondering, in your example you did not "escape" the "/" after "animal" Did you miss that, or are we not supposed to do that. Because I thought i read that you have to "escape" it with a "\" But im new at regex, so just checking thanks! Quote Link to comment https://forums.phpfreaks.com/topic/180008-solved-help-me-with-my-preg_match-plz/#findComment-949682 Share on other sites More sharing options...
cags Posted November 2, 2009 Share Posted November 2, 2009 In regular expressions you only have to escape the forward slash if that is the character you choose to use for the delimiters (the start and end character). Since you are working with a path (at least what looks like one) using the forward slash for the delimiter is not really a good idea as you'll have to do alot of escaping. I simply use the tilde (~) character as the delimiter instead. Quote Link to comment https://forums.phpfreaks.com/topic/180008-solved-help-me-with-my-preg_match-plz/#findComment-949684 Share on other sites More sharing options...
physaux Posted November 2, 2009 Author Share Posted November 2, 2009 In regular expressions you only have to escape the forward slash if that is the character you choose to use for the delimiters (the start and end character). Since you are working with a path (at least what looks like one) using the forward slash for the delimiter is not really a good idea as you'll have to do alot of escaping. I simply use the tilde (~) character as the delimiter instead. ahhh true true I forgot you can change the delimiter. Smart. Thanks for the help!!! Quote Link to comment https://forums.phpfreaks.com/topic/180008-solved-help-me-with-my-preg_match-plz/#findComment-949691 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.