1

I need to extract a specific Id from a html document but the problem is that the id must not "be used".

Here is the html content http://pastebin.com/wF2dx8JZ

As you may see there are different html blocks . Some of them contain the "Used" word so I need to extract only the first id which is not used. Basically I can write a simple pattern like : $pattern = "/javascript:tw(.*))/"; preg_match_all($pattern, $content, $matches); $id = $matches[1][0];

However in this case I'm also getting the "ids" which are used so I don't know how to exclude them from the equation . Any idea would be highly appreciated.

2
  • whaaa? Step 1: give us an example of the data you actually want returned. Step 2: Use a parser, it will make your life easier. Commented Jun 13, 2010 at 4:14
  • As you can see I need to extract the data which is between javascript:tw( and ) . An example may be 272896, 309206, 308845 etc ... The issue that I have is that some html blocks contain the "used " word ... "<font color=3300cc>Used</font>" .. and I should not extract the information from the blocks that contain the "used" word. Commented Jun 13, 2010 at 12:24

3 Answers 3

1

Try this:

if (preg_match_all('~Used.*?javascript:tw\((\d+)\)~ig', $content, $matches))
{
    print_r($matches);
}

But, you should know, there's a 99.9% chance of a better way of doing this. Do you have access to the data source?

Sign up to request clarification or add additional context in comments.

1 Comment

unfortunately your pattern doesn't return any result . I don't have access to the data source or any api ....
0

use print_r($matches)

edited:

preg_match('#\(([^)]+)\)#', $matches[1][0], $m);
echo $m[1];

2 Comments

ok and how do I extract only the info that don't have the "used" word in the html block ? if I print the matches from my pattern it returns all the Ids (that have used word in the html block and that don't have )
edited answer just echo only numbers. refer to stackoverflow.com/questions/3035258/… for other answer. :)
0

It depends a little on how your html "blocks" are stored in memory. Do you have an array of strings, each of which contains the html for one "block"? If not, can you make one by using PHP's explode() function? (For example, $html_blocks = explode("<!---->", $all_html); if that comment sequence is actually a part of your data rather than something you added.)

Once you have the blocks separated, then you can use preg_grep() to find the blocks which don't contain 'used'. So do something like this:

$unused_blocks = preg_grep("Used", $html_blocks, PREG_GREP_INVERT);

If you want to be more careful about matching, you can use another regexp as the first parameter.

Now you have $unused_blocks, which is an array of html strings that are 'not used'. You can then use your already working preg_match() pattern to extract the ids for each one.

Hope this helps, or gets you closer anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.