find string with regex in php -
i new regex , therefore gone through php manual regex , tried below:
i have string this:
c-1
c-10
c-100
, on upto c-unlimited
now want confirm string coming valid string starts c-
, after c-
contains numbers..
to work have used preg_match
$slug='c-12'; if(preg_match("/\[c-(.*?)]/",$slug,$result)){ echo "true";} else{ echo "false"; }
also this:
$pattern = '/^c-/'; if(preg_match($pattern, substr($slug,2), $matches, preg_offset_capture){ echo "true";} else{ echo "false"; }
and this:
$pattern = '/^c-/'; if(preg_match($pattern, $slug, $matches, preg_offset_capture, 3){ echo "true";} else{ echo "false"; }
and 1 more thing wanted to validate string end. below:
$slug="my-string-content-123";
here wanted validate string contains number @ end after -
example 123
but not able work... , sorry bad english..
any or suggesstion great help.. in advance..
for common (not c-
) case:
$slug="my-string-content-123"; if(preg_match('/([^0-9]+)-([0-9]+)$/', $slug, $matches)) { //prefix in $matches[1]; //the number in $matches[2]; }
Comments
Post a Comment