php - preg matching and replacing elements -
hi how do preg match on
$string1 = "[%refund%]processed_by" $string2 = "[%refund%]date_sent"
i want grab bits inside %% , remove [%item%]
altogether. leaving "proccessed_by" or "date_sent" have had go below come bit stuck.
$unprocessedstring = "[%refund%]date_sent" $match = preg_match('/^\[.+\]/', $unprocessedstring); $string = preg_replace('/^\[.+\]/', $unprocessedstring); echo $match; // should output refund echo $string; // should output date_sent
your problem use of preg_match
function. returns number of matches found. if pass variable third parameter, stores matches entire pattern , subpatterns in array.
so can capture both of parts want in subpatterns preg_match
, means don't need preg_replace
:
$unprocessedstring = "[%refund%]date_sent" preg_match('/^\[%(.+)%\](.+)/', $unprocessedstring, $matches); echo $matches[1]; // outputs 'refund' echo $matches[2]; // outputs 'date_sent'
Comments
Post a Comment