Şöyle bir dize var:
'P10005c4'
Ben bu değişkenlerin içine dizeden productId 10005 ve ColorID 4 ayıklamak gerekiyor:
$productId $colorId
productId is > 10000. colorId > 1.
Ben bu regex kullanarak temiz ve güzel nasıl yapabilirim?
Bu, aşağıdaki düzenli ifade ile mümkün olmalıdır:
/p(\d+)c(\d+)/
This basically means, match any string that consists of a p, followed by one or more digits, then followed by a c, then followed by one or more digits. The parentheses indicate capture groups, and since you want to capture the two ids, they're surrounded by them.
Amaçlar için kullanmak için, aşağıdaki gibi bir şey yapmak istiyorum:
$str = 'p10005c4';
$matches = array();
preg_match('/p(\d+)c(\d+)/', $str, $matches);
$productId = $matches[1];
$colorId = $matches[2];
Düzenli ifadeler ile başlarken daha fazla bilgi için, Regular-Expressions.info bakmak isteyebilirsiniz.