开发者

Warning: preg_match() [function.preg-match]: Unknown modifier '(' [duplicate]

开发者 https://www.devze.com 2023-03-04 05:44 出处:网络
This question already has answers here: Warning: preg_replace(): Unknown modifier (3 answers) Closed 3 years ago.
This question already has answers here: Warning: preg_replace(): Unknown modifier (3 answers) Closed 3 years ago.

I have this HTML tags :

<div class="title">Copy me if you can</div>

so I wanna use preg_match to take just "Copy me if you can" I'm using this preg pattern :

$pr开发者_如何学JAVAeg = "<div class=\"title\">(.+?)</div>";

So I wrote this code

$match = preg_match($preg,$content);

The browser outputs :

Warning: preg_match() [function.preg-match]: Unknown modifier '('

What


You forgot the delimiter

$preg = '~<div class="title">(.+?)</div>~';

The first letter in the pattern always defines the delimiter to use. In your case its <, so the ending delimiter is >. Everything after that is used as special modifier, that changes specific behaviours. ( is not a valid modifier. Thats what the error message wanted to tell you :)


Try : $preg = '/<div class="title">([^<]+)</div>/';


Personally, when HTML is involved I'd steer away from the defacto delimiter / and use # instead so you don't have to remember to escape any closing HTML tags.

$preg = '#<div class="title">([^<]+)</div>#';


You need to add delimites to your regex. Try this:

$preg = "/<div class=\"title\">(.+?)<\/div>/";

/ are delimiters which marks the start and stop of your regex. The reason for delimiters is so that you can add flags to your regex. Those flags are inserted after your regex. Example:

$preg = "/<div class=\"title\">(.+?)<\/div>/i";

I've added i as a modifier, marking the regex search as case insensitive.


I have the same problem and wonder if I could do the same, ex.: Warning: preg_match() [function.preg-match]: Unknown modifier 't' in /home/08/public/www/wp-content/plugins/woocommerce-gateway-dibs-form/gateway-dibs.php on line 707

This is the code: if ( preg_match($_SERVER["REQUEST_URI"], 'woocommerce/dibscancel') !== false) {

        header("HTTP/1.1 200 Ok");

        $callback = new WC_Gateway_Dibs;
        $callback->cancel_order(stripslashes_deep($_REQUEST));
        return;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消