2

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

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Adam
  • 21
  • 1
  • 2
  • http://stackoverflow.com/questions/1531456/is-there-a-php-function-that-can-escape-regex-patterns-before-they-are-applied – zloctb Oct 27 '15 at 11:19

5 Answers5

4

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

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • it Works ! thanks (: , but is there any RegExp that only takes what's between the tags ? (Copy me if you can) , my RegExp takes the whole tag :/ I know i can use str_replace() to replace the tags with nothing but a RegExp that can do the job is better xD – Adam May 10 '11 at 09:50
1

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;
    }
Art
  • 11
  • 1
1

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.

alexn
  • 57,867
  • 14
  • 111
  • 145
  • 1
    Don't you need to escape the / in your closing div? Also, case-sensitivity isn't an issue when you're matching dot – Nev Stokes May 09 '11 at 19:35
  • @Nev It was just an example for explaining delimiters. – alexn May 09 '11 at 19:46
  • 1
    Understood, but it wouldn't work were someone to actually try it. Without using <\/div> you'd just confuse those who are unfamiliar. – Nev Stokes May 09 '11 at 19:48
1

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

David Fells
  • 6,678
  • 1
  • 22
  • 34
1

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>#';
Nev Stokes
  • 9,051
  • 5
  • 42
  • 44