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

淺唱寂寞╮ 提交于 2019-12-11 03:13:16

问题


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 :

$preg = "<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


回答1:


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 :)




回答2:


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




回答3:


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>#';



回答4:


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.




回答5:


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;
    }


来源:https://stackoverflow.com/questions/5941273/warning-preg-match-function-preg-match-unknown-modifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!