daydreamer Posted October 8, 2009 Share Posted October 8, 2009 Hi, I have data in this format: <td>should be matched 2nd thisis728whatineed</td> I want to match "thisis728whatineed" 1st, and then store it, then secondly store "should be matched 2nd". The number of spaces in there will vary, but "thisis728whatineed" will always be numbers and letters with no spaces. So I need something like this, but im not sure about how to make it work: <?php preg_match_all("~<td>(.*)(\D*)\s?</td>~i", $xxx, $matches); ?> Im guessing i need to use $ to start at the end of the string first, but how do I do this? Thanks for any help with this. Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/ Share on other sites More sharing options...
Garethp Posted October 8, 2009 Share Posted October 8, 2009 "~\s([a-zA-Z0-9])\s?</td>$~" Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/#findComment-933355 Share on other sites More sharing options...
JAY6390 Posted October 11, 2009 Share Posted October 11, 2009 '%<td>([^<]+)([a-z0-9])</td>%si' thisis728whatineed will be in $matches[2] the rest will be in $matches[1] (without the <td> and </td>) Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/#findComment-934921 Share on other sites More sharing options...
nrg_alpha Posted October 11, 2009 Share Posted October 11, 2009 daydreamer, with regards to your pattern, using .* will cause backtracking, which should be avoided when possible (for speed and accuracy reasons - you can read about there here: post #11 and #14 explains this [it disucsses .+ instead, but pretty much the same principal). Also note that \D is a non digit character.. but since the last part contains letters and numbers, making use of this wouldn't work. Am I correct in assuming that the 'should be matched 2nd' part is also dynamic? If so, we have 2 sections to the td tag that are always different... For example: $html = <<<EOF <td>should be matched 2nd thisis728whatineed</td> <td>Some garbage</td> <td id="whatever">should be also be matched 2nd thisis886whatineed2</td> EOF; preg_match_all('#<td[^>]*>(.+?)\s([a-z0-9]+)</td>#i', $html, $matches, PREG_SET_ORDER); foreach ($matches as &$val) { array_shift($val); // ditch initial element 0, as this simply represents the entire match rsort($val); } echo '<pre>'.print_r($matches, true); As you can see from the grouped output, 'garbage' and 'some' is also included.. Something tells me you need to provide more information on the circumstances involved, otherwise it is too 'generic' and you'll most likely get undesirable results. Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/#findComment-934944 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 I believe that the 'thisis' and 'whatineed' surrounding the number were only put there to signify the OP wanted the number in the center meaning that the pattern should have been \d rather than \D, but I could be wrong. Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/#findComment-934947 Share on other sites More sharing options...
nrg_alpha Posted October 11, 2009 Share Posted October 11, 2009 I believe that the 'thisis' and 'whatineed' surrounding the number were only put there to signify the OP wanted the number in the center meaning that the pattern should have been \d rather than \D, but I could be wrong. My understanding is that it can be a mish-mash of letters or numbers: ...but "thisis728whatineed" will always be numbers and letters with no spaces. If you are correct, then the OP has not stated things correctly. Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/#findComment-934953 Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 No, my bad, I think your right, I must have skipped that bit Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/#findComment-934954 Share on other sites More sharing options...
nrg_alpha Posted October 11, 2009 Share Posted October 11, 2009 Come to think of it, I should not have used rsort, as this sorts by values, not keys. A solution to this could be to change the inside of the foreach loop from: array_shift($val); // ditch initial element 0, as this simply represents the entire match rsort($val); to: array_shift($val); // ditch initial element 0, as this simply represents the entire match krsort($val); // sort array be keys in reverse order (maintains key association) $val = array_merge($val); // in essence resets key association Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/#findComment-934980 Share on other sites More sharing options...
daydreamer Posted October 15, 2009 Author Share Posted October 15, 2009 "~\s([a-zA-Z0-9])\s?</td>$~" A variation of this worked, thanks, just needed to know how to use the $. daydreamer, with regards to your pattern, using .* will cause backtracking, which should be avoided when possible Am I correct in assuming that the 'should be matched 2nd' part is also dynamic? Yes, should be matched 2nd will be dynamic. But you use (.+?) in your example reg exp! Thanks for your suggestions. The majority of the data is how I need it, but I will manually go through and change the ones which will take longer to write a reg exp for because they are so different to what I am currently matching. Quote Link to comment https://forums.phpfreaks.com/topic/177021-solved-match-something-starting-at-the-end-of-a-string/#findComment-937649 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.