AndyPSV Posted December 7, 2011 Share Posted December 7, 2011 all the same: http://www.phpfreaks.com/forums/index.php?topic=349404.0 <?php $variable = 'answer_comment'; preg_match("/(?<=$variable)[^A-Za-z]+/sm", $subject, $match); (with code that worked) but instead of only numbers, I've got now: (code above only selects to first appearance of: "#", then it stops) 123321#c31221 12#c15 21321#c2153 How to extract such data with the usage of this modified preg_match? Thank you. Quote Link to comment Share on other sites More sharing options...
joe92 Posted December 7, 2011 Share Posted December 7, 2011 Will the only non digits always be '#c'? Or could it be '#g' or even '$Ylpqw'? Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted December 7, 2011 Author Share Posted December 7, 2011 both they could also be extended to "#c211:9494" (for additional params), in the future thank you. Quote Link to comment Share on other sites More sharing options...
joe92 Posted December 7, 2011 Share Posted December 7, 2011 Hmm, not entirely sure on what you need now However, something like this might work. Again, untested: <?php $variable = 'answer_comment'; preg_match("/(?<=$variable)(\d+#[a-zA-Z]\d+(:\d+)?)+?(?![a-zA-Z]{2})/sm", $subject, $match); That will look for: - Something that begins with the variable. - Is constantly looking ahead to make sure that: Two letters don't match (indicates a word) - The following will happen between one and infinity times - Starts with a digit. - There is at least one and infinity digits. - Followed by a hash symbol - Followed by one letter, upper or lower case - Followed by between one and infinity digits - Between one and zero times the following will happen - Followed by a colon - Followed by between one and infinity digits If you have another variation of a regex question, before asking it, please go to http://www.regular-expressions.info/tutorial.html and read up on regex. If, if, you still cannot understand how to get what you need, then ask the question. Hope this helps, Joe Quote Link to comment Share on other sites More sharing options...
AndyPSV Posted December 8, 2011 Author Share Posted December 8, 2011 It doesn't wants to work. <?php $CID = 'question_comment'; $s = 'answer_comment 21221#c321321321 213231321213 21332132121121212112 122121434 12 question_comment 1#c8 5093320#c21 3215533 '; preg_match("/(?<=$CID)(\d+#[a-zA-Z]\d+(:\d+)?)+?(?![a-zA-Z]{2})/sm",$s,$arr); /*$arr = trim($arr[0]); $arr = explode(NL,$arr);*/ var_export($arr); die; ?> I think, I would use explode(), however: it's not so elegant as the preg_match(). --------------------------------- (didn't thought that before) <?php $CID = 'question_comment'; $s = 'answer_comment 21221#c321321321 213231321213 21332132121121212112 122121434 12 question_comment 1#c8 5093320#c21 3215533 '; $arr1 = explode($CID,$s); $arr = explode("\r\n\r\n",$arr1[1]); #preg_match("/(?<=$CID)(\d+#[a-zA-Z]\d+(:\d+)?)+?(?![a-zA-Z]{2})/sm",$s,$arr); /*$arr = trim($arr[0]); $arr = explode(NL,$arr);*/ var_export($arr); die; ?> SOLVED, thank you. Quote Link to comment 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.