<?php $s = 'KRS-KRS-454-L'; echo preg_replace('/^(\w+)-(?=\1)/', '', $s); ?>
A positive result is used to check duplicate lines (?=...) .
Note that \w also contains an underscore. If you want to limit only to alphanumeric characters, use [a-zA-Z0-9] .
In addition, I got attached to ^ , as you already mentioned: "Duplicate characters will always occur in front [...]"
mhyfritz
source share