Rifts Posted March 6, 2012 Share Posted March 6, 2012 I have tried a ton of different ways to get text between tags with no luck. Here is the code preg_match_all('/<td class=\"word fixed_text\".*td>/', $result, $matches1); I'm trying to get the text between <td class="word fixed_text"> *HERE* </td> can anyone see why that isnt working? when I echo out the $result everything is there so I know thats not the problem. thanks Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted March 6, 2012 Share Posted March 6, 2012 What exactly is the issue? Your code works fine for me, it matches the whole TD ... /TD space. If you're actually trying to match INSIDE the tags, you need to add the > and < and use parens to make a capture group. Quote Link to comment Share on other sites More sharing options...
batwimp Posted March 6, 2012 Share Posted March 6, 2012 preg_match_all('/<td class=\"word fixed_text\">(.*)<.*td>/', $result, $matches1); Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted March 6, 2012 Share Posted March 6, 2012 $pattern = '~<td class="word fixed_text">([^<]*)</td>~i'; Quote Link to comment Share on other sites More sharing options...
ragax Posted March 6, 2012 Share Posted March 6, 2012 Hi Rifts! Interesting, you have exactly the same problem as Codemunkie on the other post from today. This is an old, classic problem (which I've more fully described on my page about greedy and lazy quantifiers). Basically, your dot-star (.*) before "td" is "greedy": it will cause the regex engine to match every single character until the end of the string. Then, to match "td", the engine will roll back character by character until it finds a "td", which will be the very last "td" on your string. This means the match may swallow many tags in a row. If you want to use a dot-star for this task (and there are multiple ways of doing it), that is fine, but you need to make the star lazy by adding a question mark. Here is some working code: Code: <?php $string=' <td class="word fixed_text">HERE</td> <td class="word fixed_text">HERE 2</td> <td class="word fixed_text">HERE TOO</td> '; $regex=',class="word fixed_text">(.*?)</td>,'; preg_match_all($regex, $string, $matches, PREG_PATTERN_ORDER); $sz=count($matches[1]); for ($i=0;$i<$sz;$i++) echo $matches[1][$i]."<br />"; ?> Output: HERE HERE 2 HERE TOO Please let me know if you have any questions! 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.