I'm trying to work out a quick and simple way to remove the first two digits and a decimal from a string, if that is how it is made up.
I am half way there but need help to finish.
So (first is what I start with, 2nd is the result):
xx.yyy = yyy
aaaaa = aaaaa
test.hello = test.hello
a.test.b.x = a.test.b.x
aa.bb.cc = bb.cc
So it only removes 2 digits and a decimal if it exists like that. If it is three digits and a decimal then it isn't removed.
This is where I am so far:
$string = 'xx.hello';
$pattern = '/(2-digits)./i';
$replacement = ''; // remove if matched
echo preg_replace($pattern, $replacement, $string);
?>
This will do letters, digits, and underscores:
preg_replace('/^\w{2}\./', '', $string);
Without numbers or underscores, both upper and lowercase:
preg_replace('/^[a-zA-Z]{2}\./', '', $string);
Assuming by "digits" you mean actual digits (0-9) and by "decimal" a dot:
$string = preg_replace('/^\d{2}\./','',$string);
Try the following:
$pattern = "~^[0-9A-Za-z]{2}\\.~";
It matches two alphanumerical characters at the beginning of the string, followed by a period (.). Notice that the period has been escaped so the period is interpreted as a literal. (Otherwise, the period matches any single character.)