0

I'm doing parsing some data from HTML codes using preg_match_all() function. in order to make it easier, I use regex tool to do it. so, my pattern is like:

<td class="(weekday|weekend) reservation (primary|alternate) fixwidth calday fixwidth " >(.*?)</td>

when I use it in regex tool I got the result. but when I put into preg_match_all() function, I did not get the result. I've tried running it on online PHP function tester (functions-online.com/preg_match_all.html), the pattern give null result with the message unknown modifier 'w'.

sorry.., this is how I call use preg_match_all function:

preg_match_all('|<td class="(weekday|weekend) reservation (primary|alternate) fixwidth calday fixwidth " >(.*?)</td>|', $v, $matches3, PREG_PATTERN_ORDER);

what makes it has different result? and how to get result using preg_match_all() with that pattern?

I hope somebody help me. Thanks before.

Rijalul fikri
  • 41
  • 1
  • 8
  • How are you using the regex in your code? Can you show the code snippet with `preg_match_all`? – Jerry Sep 10 '13 at 15:23
  • Can you show us how are you calling the `preg_match_all()` function ? – Ibrahim Najjar Sep 10 '13 at 15:24
  • `preg_match_all('|(.*?)|', $v, $matches3, PREG_PATTERN_ORDER);` – Rijalul fikri Sep 10 '13 at 15:35
  • thank you, I already got the result. – Rijalul fikri Sep 10 '13 at 15:39
  • **Don't use regular expressions to parse HTML. Use a proper HTML parsing module.** You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php or [this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Sep 10 '13 at 15:56
  • You just must escape the last `/` in your regex... Anyway, please accept one of the given answers below... Also consider using [regex101](http://regex101.com/) in the furtur, it's my favourite and never had any such problems with :) – Enissay Sep 10 '13 at 17:19
  • possible duplicate of [PHP, Error preg\_match\_all error unknown modifier?](http://stackoverflow.com/questions/7422466/php-error-preg-match-all-error-unknown-modifier) – Toto Sep 11 '13 at 11:47

2 Answers2

3

preg_match_all('/<td class="(weekday|weekend) reservation (primary|alternate) fixwidth calday fixwidth " >(.*?)<\/td>/', $stringToSearch, $results)

First, you have to use delimiters, eg. / to wrap regexp.

Second, if you use / as delimiter, you have to escape every occurence of it in provided regexp.

Elon Than
  • 9,603
  • 4
  • 27
  • 37
1

As I can see you are using | as the regular expression delimiter but it is interfering with the OR operator | used inside the regular expression, just call the function like this:

 preg_match_all('/<td class="(weekday|weekend) reservation (primary|alternate) fixwidth calday fixwidth " >(.*?)</td>/', $v, $matches3, PREG_PATTERN_ORDER);

This is how PHP is imaging your orginal expression: |<td class="(weekday|w then it assumes that the w after the pipe character | is regular expression modifier but w isn't, hence the terrific problem.

WARNING: Be very aware that you shouldn't use regular expressions to parse HTML, use an HTML parser instead.

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95