php - preg_match not returning expected results -


i'm attempting use regexp parse search string time time may contain special syntax. syntax im looking [special keyword : value] , want each match put array. keep in mind search string contain other text not intended parsed.

$searchstring = "[startdate:2010-11-01][enddate:2010-11-31]"; $specialkeywords = array(); preg_match("/\[{1}.+\:{1}.+\]{1}/", $searchstring, $specialkeywords); var_dump($specialkeywords); 

output:

array(1) { [0]=> string(43) "[startdate:2010-11-01] [enddate:2010-11-31]" }

desired output:

array(2) { [0]=> string() "[startdate:2010-11-01]"

[1]=> string() "[enddate:2010-11-01]"}

please let me know if not being clear enough.

your .+ matches across boundaries between 2 [...] parts because matches character, , many of them possible. more restrictive characters may matched. {1} redundant , can dropped.

/\[[^:]*:[^\]]*\]/ 

should work more reliably.

explanation:

\[     # match [ [^:]*  # match number of characters except : :      # match : [^\]]* # match number of characters except ] \]     # match ] 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -