Remove everything before certain point of a string

85 Views Asked by At

I have stored strings under name column in my database like so:

materials

302 / 1001 / 8 ДЮБЕЛ 8/60
304 / 1001 / 32 НИВИЛИР АЛУМИНИЕВ AV 180
232 / 1004 / 34 РЕДУЦИР-ВЕНТИЛ ЗА КИСЛОРОД А ТИП
549 / 1034 / 10051 ТРЪБА НЕРЪЖДАЕМА ШЕВНА 3/4"
455 / 1034 / 10053 ТРЪБА НЕРЪЖДАЕМА ШЕВНА 1 1/4"
603 / 7000 / 10086 ТРЪБА ПОДЦИНКОВАНА 2"
333 / 4001 / 10101 ТРЪБА ППР Ф 25 ММ.
344 / 1002 / 10102 ТРЪБА ППР Ф 20 ММ.

The name of the material begins after the 3rd number and I'd like to remove everything before the actual name including the whitespace.

The new table should look like this:

materials

ДЮБЕЛ 8/60
НИВИЛИР АЛУМИНИЕВ AV 180
РЕДУЦИР-ВЕНТИЛ ЗА КИСЛОРОД А ТИП
ТРЪБА НЕРЪЖДАЕМА ШЕВНА 3/4"
НЕРЪЖДАЕМА ШЕВНА 1 1/4"
ПОДЦИНКОВАНА 2"
ТРЪБА ППР Ф 25 ММ.
ТРЪБА ППР Ф 20 ММ.

I have tried to achieve this in a few ways but none of them worked as expected. strstr for example removes everything before an occurrence like / but it only removes the first number. Regular expressions didn't work either.

2

There are 2 best solutions below

3
Illia Nova On BEST ANSWER

If you want to remove all the numbers and slashes before the text (letters), you can do as in the example:

<?php

// Sample array of strings
$originalStrings = [
    "302 / 1001 / 8 ДЮБЕЛ 8/60",
    "304 / 1001 / 32 НИВИЛИР АЛУМИНИЕВ AV 180",
    "232 / 1004 / 34 РЕДУЦИР-ВЕНТИЛ ЗА КИСЛОРОД А ТИП",
    "549 / 1034 / 10051 ТРЪБА НЕРЪЖДАЕМА ШЕВНА 3/4\"",
    "455 / 1034 / 10053 ТРЪБА НЕРЪЖДАЕМА ШЕВНА 1 1/4\"",
    "603 / 7000 / 10086 ТРЪБА ПОДЦИНКОВАНА 2\"",
    "333 / 4001 / 10101 ТРЪБА ППР Ф 25 ММ.",
    "344 / 1002 / 10102 ТРЪБА ППР Ф 20 ММ."
];

// Process each string
foreach ($originalStrings as $originalString) {
    // Use regular expression to match and replace unnecessary numbers
    $result = preg_replace('/^[0-9\s\/]+/', '', $originalString);
    
    echo $result . PHP_EOL;
}

?>

This code uses preg_replace to replace all leading numbers, whitespace and slashes with an empty string. The result is the modified strings without the unnecessary numbers before the text.

1
Sammitch On

Explode with a field count, and trim the whitespace.

$input = <<<_E_
302 / 1001 / 8 ДЮБЕЛ 8/60
304 / 1001 / 32 НИВИЛИР АЛУМИНИЕВ AV 180
232 / 1004 / 34 РЕДУЦИР-ВЕНТИЛ ЗА КИСЛОРОД А ТИП
549 / 1034 / 10051 ТРЪБА НЕРЪЖДАЕМА ШЕВНА 3/4"
455 / 1034 / 10053 ТРЪБА НЕРЪЖДАЕМА ШЕВНА 1 1/4"
603 / 7000 / 10086 ТРЪБА ПОДЦИНКОВАНА 2"
333 / 4001 / 10101 ТРЪБА ППР Ф 25 ММ.
344 / 1002 / 10102 ТРЪБА ППР Ф 20 ММ.
_E_;

$lines = explode("\n", $input);

foreach($lines as $line) {
    var_dump(array_map('trim', explode('/', $line, 3)));
}