Domanda

I try to use preg_match to find the phrase: "[image: Inline images num]" in the text Unfortunately I could not,

this is my try:

preg_match('/^[image(.*)]$/',$text);

thank you for your help!

È stato utile?

Soluzione

You have to escape your [] as follows:

preg_match('/^\[image(.*)\]$/',$text);

Also, as @h2ooooooo said, this will have better performance:

^\[image[^\]]+\]$

It works now. You can see it here --v

Demo: http://regex101.com/r/iK0nE9

Altri suggerimenti

if (preg_match('/\[image:.*?\]/i', $subject)) {
    # Successful match
} else {
    # Match attempt failed
}


Match the character “[” literally «\[»
Match the characters “image:” literally «image:»
Match any single character that is not a line break character «.*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “]” literally «\]»
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top