Web >> Development >> PHP >> How to extract certain patterns of a string using regular expressions

<?php $string = "{home,http://a.b.c/home}"; preg_match("/^[{]([^,]+),(.*)[}]$/",$string,$matches); echo "$matches[1]\n"; echo "$matches[2]\n"; // note that $matches[0] contains the entire string if a match is found and // $matches[1] onwards contains sub-section matches. in this example $matches[1] = "home"; $matches[2] = "http://a.b.c/home" ?>