Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/258400-going-insane/
Share on other sites

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!

:)

 

Link to comment
https://forums.phpfreaks.com/topic/258400-going-insane/#findComment-1324583
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.