-1

i have a question about preg_match. I have a textfield, where users can fill in random text. However when they type:

[IMG]image url here[/IMG]

i want everything between [IMG][/IMG] to be put in a variable.

What i have now:

if(isset($_POST['edit_signature_table'])){
    $string = $_POST['thread_message'];
    echo $string;

    if (preg_match('/[IMG](.*?)[/IMG]/', $string, $display)) {
        print_r($display);
    }else{
        echo "[IMG][/IMG] was not used.";   
    }
}

When i post something in the form and press submit. i get the result i just filled in. (done by echo $string).

But. i always get this result:

This is typed in the textfield. and contains [IMG]an image[/IMG] 
Warning: preg_match(): Unknown modifier 'I' in C:\xampp\htdocs\Proeven\Forum\profile.php on line 124 
[IMG][/IMG] was not used.

As you can see. The first line is me result from $string.

Then i get an error (line is at if(preg_match))

And then it goes to the else statement.

However in my result, you can see that i used [IMG][/IMG] and i expect the result to be: an image

Can someone tell me what i'm doing wrong?

EDIT:

people downvoting and saying it is a duplicate.

I looked at other peoples questions about this, but still did not know how to solve my problem.

People downvoting. Please tell me why... The question is clear enough i think.

Mitch
  • 1,173
  • 1
  • 10
  • 31
  • 1
    Escape the `[` and `/` -> `\[`, `\/IMG` - `'/\[IMG](.*?)\[\/IMG]/'` – Wiktor Stribiżew Sep 20 '16 at 09:28
  • Might be a duplicate but saw that and still did not know what to do :/ Also people downvoting. Please tell me why. Explanation was clear enough i think. And yes i watched other questions and searched the internet. But still did not know what to do. – Mitch Sep 20 '16 at 09:34
  • It is clear what to do: escape special characters and pattern characters that are used as regex delimiters. – Wiktor Stribiżew Sep 20 '16 at 09:40
  • Since this is the first time i'm using pregmatch i did not know that. Also a lot of questions are getting marked as duplicate. In the future no one will be able to ask a question beause everything has been asked already before. Even though some people (like me) don't know what to do even when seeing an other answer. Little but unfair or no? – Mitch Sep 20 '16 at 09:42

1 Answers1

1

You need to escape your regex in order for it to work.

preg_match('/[IMG](.*?)[/IMG]/', $string, $display)

becomes

preg_match('/\[IMG\](.*?)\[\/IMG\]/', $string, $display)

you can also easily try your regex on different inputs here: http://www.phpliveregex.com/

Fabian Bettag
  • 799
  • 12
  • 22