ParisNakitaKejser
ParisNakitaKejser

Reputation: 14959

preg_match on special characters

I’m trying to make a preg_match in my PHP code, but I can't seem to get it to work.

I have these 3 tags

{$success url='http://www.domain.localhost/form-success'}
{$error url='http://www.domain.localhost/form-error'}
{$button title='send form'}

How can I get my preg_match to accept what I’m trying to do?

I’m trying to get {$button(*)} out to my match path, and the same I want to do with the 2 others {$success(*)} and {$error(*)}

Now I think it should look like this

preg_match("/\{\$button(.+?)}/s", $content, $match);

But it still isn’t working, so I hope other people can help me here.

Upvotes: 0

Views: 459

Answers (3)

anubhava
anubhava

Reputation: 786146

You need to escape the closing } and need to use preg_match_all to match all the lines.

Try this regex:

preg_match('/\{\$(?:success|error|button)\s+([^}]+)\}/i', $content, $match );

Working Demo: http://ideone.com/fKq5L2

OUTPUT:

Array
(
    [0] => {$success url='http://www.domain.localhost/form-success'}
    [1] => {$error url='http://www.domain.localhost/form-error'}
    [2] => {$button title='send form'}
)

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324800

\$ already means "literal $" in PHP strings, so when put in a regex it just means "end of string".

Try \\\$ instead.

Upvotes: 1

cubitouch
cubitouch

Reputation: 1937

I think the correct regex to retrieve the 'button' tag should be : \{\$button([^\}]*)\}

You can try your expression on http://regexpal.com/

So with php :

preg_match("/\{\$button([^\}]*)\}/s", $content, $match );

Upvotes: 1

Related Questions