Jump to content

[SOLVED] Match something starting at the end of a string.


daydreamer

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

"~\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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.