Usman Khan
Usman Khan

Reputation: 359

confused about the preg_match expression

preg_match uses an expression of casses like

preg_match_all("/\[(.*?)\]/", $content, $matches); 

so I am confused about creating the patterns how is this developed /[(.*?)]/ as I am trying to create a shortcode and want if any one places the short code any where in the page I can get the shortcode from there and clean up and know what shortcode is placed like [shortcode app='slider' id='2'] i need slider and 2 from the shortcode so I can show the slider and replaced the slider with the shortcode

Upvotes: 3

Views: 49

Answers (1)

Here is the explanation

\[ - matches character with "[". "\" is used to escape it because we have special meaning for it in regular expression.

( - ) will capture the part inside to be used as a back reference while we replace

.* - any character can be present here except line breaks and it can be 0 or more times.

? - makes the previous part optional (which is not necessary here, cause using * already takes care of it, which means 0 or more times.)

\] - matches one character "]"

Learn more on regular expressions from here : http://www.regular-expressions.info/

Upvotes: 3

Related Questions