There is a string:
test-test-example-demo-1a-3321-0-3555-0
I need to delete everything except the numbers, but that 1a also deleted, since with this figure there is a different symbol from –
I did this:
preg_replace ('/ [^ \ d -] /', '', $ STR);
But in the end I get:
1-3321-0-3555-0
And you need to get:
3321-0-3555-0
Number of words in the string is always different.
I will be grateful for the help.
Answer 1, Authority 100%
One line by regular (if necessary exactly) :
$ text = 'test-test-example-demo-1a-3321-0-3555-0';
Echo preg_replace ('~ ^. *? ((?: \ d + -) + \ d +). *? $ ~', '$ 1', $ text);
Answer 2
That solution I got without regular:
$ string = 'test-test-example-demo-1a-3321-0-3555-0';
$ Array = Explode ('-', $ String);
Foreach ($ Array As $ Key = & GT; $ value) {
If (! is_numeric ($ value)) {
Unset ($ Array [$ Key]);
}
}
Print_R (implode ('-', $ array));
Result: 3321-0-3555-0
Answer 3
$ str = 'test-test-example-demo-1a-3321-0-3555-0-';
$ str = preg_replace ('/ [0-9] + [a-za-z] /', '', $ STR); // Remove all the numbers after which there are letters
$ str = preg_replace ('/ [a-za-z] [0-9] * /', '', $ STR); // Remove all letters and numbers after them if they are
$ str = preg_replace ('/ - (? = -) /', '', $ STR); // Remove all the remaining pairs '-'
$ str = preg_replace ('/ ^ - /', '', $ STR); // Delete '-' at the beginning of the line
$ str = preg_replace ('/ - $ /', '', $ str); // Remove '-' at the end of the line
Echo $ str; // You can certainly collect all regular expressions in one line, but it's easier for me