This question already has an answer here:
I'm trying to extract some text in between a Request XML tag (between and tags) using this Regex:
(?<=(<Request>)).*(?=(</Request>))
RegexBuddy shows me that it's fine, but preg_match returns this error: "Unknown modifier R". Placing a backslash before the "/" makes it so nothing returns. Does anyone know what the problem is?
Code:
$parsedQuery = fopen("c:\\query", "r") ;
$parsed ="".
while (!feof($parsedQuery)) {
$parsed .= fgets($parsedQuery) ;}
$reg = "#(?<=(<Request>)).*(?=(</Request>))#";
$match = array();
preg_match($reg, $parsed, $match);
print_r($match);
Edit: I now noticed that the file opens with an unidentified character (binary value is 3F) after the opening of each tag (the "<" character). I assume php's fgets implementation does this for security measures, could this be the problem, and is there any way to surpass it?
</div>
The problem is that in PHP (and in a lot of langugages) a regexp is something like /pattern/modifier
where modifier could be, for example, g
(multiple match). If you want to use /
in your regexp you have to escape them with a \
:
/(?<=(<Request>)).*(?=(<\/Request>))/
See http://www.php.net/manual/en/pcre.pattern.php for more information about patterns in PHP.