0

how I can join these two patterns to fit on a single preg_match_all: I have this:

preg_match_all("/(<span[^>]*annot[^>]*value=.?(\w*).?[^>]*>)(.*?)<\/span>/", $text, $matches, PREG_OFFSET_CAPTURE);

and this:

preg_match_all("/(<span[^>]*value=.?(\w*).?[^>]*annot[^>]*>)(.*?)<\/span>/", $text, $matches, PREG_OFFSET_CAPTURE);

I need the pattern to look for two things at once

Martin
  • 1,282
  • 1
  • 15
  • 43
  • 1
    you could start by NOT using regexes to try and match html. down that road [lies madness](http://stackoverflow.com/a/1732454/118068) – Marc B Jan 21 '13 at 17:55
  • thanks, but you have a better way to do it? did not do it is not a best solution – Martin Jan 21 '13 at 18:01
  • what are you trying to do? extract the contents of a couple particular ``s? – Marc B Jan 21 '13 at 18:10

2 Answers2

3

Despite the HTML matching thing, try this:

preg_match_all("/((<span[^>]*annot[^>]*value=.?(\w*).?[^>]*>)(.*?)<\/span>|(<span[^>]*value=.?(\w*).?[^>]*annot[^>]*>)(.*?)<\/span>)/", $text, $matches, PREG_OFFSET_CAPTURE);

They are just combined via an or operator like this:

(exp1|exp2)

Edit: If I understood what you are trying to do, matching "annot= value=" and "value= annot=", you can use this regexp instead (Hope I didn't mess anything, but you should get the idea):

(<span[^>]*(value=.?(\w*).?[^>]*|annot[^>]*)>)(.*?)<\/span>
Rudolf
  • 1,856
  • 2
  • 19
  • 32
2
preg_match_all("/(<span[^>]*(?:(?:annot|value=.?(\w*).?)|(?:value=.?(\w*).?|annot))[^>]*>)(.*?)<\/span>/", $text, $matches, PREG_OFFSET_CAPTURE);

Anyway, I suggest you to use PHP [Document Object Model][1] instead because it's highly discouraged to use regular expressions to parse HTML data.

<?PHP

   $DOM = new DOMDocument;
   $DOM->loadHTML($HTML);

   $items = $DOM->getElementsByTagName('span');

   foreach ($items as $item)
   {
       foreach ($item->attributes as $attribute)
       {
           if (($attr->nodeName == "name") && ($attr->nodeName == "annotation"))

            //....
?>
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98