Mitch Mitch - 1 year ago 83
HTML Question

preg_match displaying nothing/Unknown modifier 'I'

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:

$string = $_POST['thread_message'];
echo $string;

if (preg_match('/[IMG](.*?)[/IMG]/', $string, $display)) {
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?


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.

Answer Source

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

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


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

you can also easily try your regex on different inputs here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download