0

I got a bunch of XML files that contain an ID. e.g.

<WINE_PRODUCER>9456</WINE_PRODUCER>

I need to check, e.g. which file contains a WINE_PRODUCER id=9456:

$content = file_get_contents("my.xml");
$Num = 9456;
$pattern = "<WINE_PRODUCER>$Num<\/WINE_PRODUCER>";
$res = preg_match("$pattern", $content);

I got error PHP Warning: preg_match(): Unknown modifier '9'

basically it does not like numbers in the pattern.

What do I do? googled a lot but all lead to matching a group of numbers...

PS: I do not want to use DOM xml parser in this case due to performance.

2
  • Are you on a Unix/Linux platform and can you make use of tools such as grep? Commented Aug 5, 2013 at 15:49
  • you can use strpos to accomplish the same thing. $res = strpos("<WINE_PRODUCER>$Num<\/WINE_PRODUCER>", $content); To test: if ($res !== false){ // found it } Commented Aug 5, 2013 at 15:52

2 Answers 2

1

If you always need to match a fixed number you can just do a strstr.

$num = 9456;
$find = "<WINE_PRODUCER>". $num . "</WINE_PRODUCER>";
$found = strstr($content, $find) !== false;

To fix your regular expression, you need to specify delimiters:

$pattern = "@<WINE_PRODUCER>$Num<\/WINE_PRODUCER>@";

should work, but you probably don't need a regexp.

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

Comments

1

Try:

$pattern = sprintf(preg_quote('<WINE_PRODUCER>%s<\/WINE_PRODUCER>'), $num);

echo '/'.$pattern.'/';

1 Comment

preg_quote() is poorly implemented in this unexplained answer because preg_quote() does not escape forward slashes by default -- that pattern delimiter must be nominated as its second parameter.

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.