digitalzoomstudio
digitalzoomstudio

Reputation: 1132

Regex issue with php negative look behind for slash

I always get this error when using a valid lookbehind with slash

Warning: preg_match_all(): Compilation failed: missing ) at offset 22 in [...][...] on line 6

Which is weird because on phplive regex it works perfect http://www.phpliveregex.com/p/iiM but not on php 7 localhost

code is

<?php

$str = '[dzspgb_element text="<p><iframe src=\"https:/www.facebook.com/plugins/th=\"250\" height=\"500\" none=\"\" " kill_tinymce="on" type_element="text"][/dzspgb_element]';


preg_match_all("/(\w*?)=\"(.*?)(?<!\\)(\")/sm", $str, $matches);

print_r($matches);

You can test it here - http://sandbox.onlinephpfunctions.com/

Any idea what is wrong ? Maybe php 7 changed something in regex assertion ?

Upvotes: 1

Views: 262

Answers (1)

leoap
leoap

Reputation: 1729

Your problem is this:

You have:

(\w*?)=\"(.*?)(?<!\)(\")

The correct is:

(\w*?)=\"(.*?)(?<!)(\")

pay attention: (?<!\) That slash is escaping the ) so it isn't being understood as a )

EDIT: I've learned that PHP needs some attention on escaping backslashes inside a string, see the Notes on top of this manual page: http://de.php.net/manual/en/regexp.reference.escape.php

So, to correctly escape your slashes, your code, with single quotes should be:

preg_match_all('/(\w*?)=\"(.*?)(?<!\\\\)(\")/sm', $str, $matches);

Upvotes: 1

Related Questions