I have a dynamic strings that outputs,
test-only
test-only-1
test-only-12
sample-only-2
sample-test-55
etc...
I would like to ask, how can i remove -1,-12,-2, -55 dynamically using php?
Thanks!
I have a dynamic strings that outputs,
test-only
test-only-1
test-only-12
sample-only-2
sample-test-55
etc...
I would like to ask, how can i remove -1,-12,-2, -55 dynamically using php?
Thanks!
This will work when all of the input is on the same line.
preg_replace('/([a-z\-]+)(-[0-9]+)/', '$1', $data);
With input on multiple lines it would be best to anchor to the end of the string
preg_replace('/^([a-z\-]+)(-[0-9]+)$/', '$1', $data);
You can use preg_replace to replace with a regex. $string in the example below would be the text you are trying to fix. This will replace - and /d+ will replace numeric characters.
$pattern = '/\-/d+$/i';
print preg_replace($pattern, '', $string);