There is a line xxxxxxxxx 10.12.2012 xxxxxx.

You must delete everything from the date

preg_replace("/([0-2]\d|3[01])\.(0\d|1[012])\.(\d{1,4})$/", " ",$pass_agensy); 

Tell me, please, what is the error?

    2 answers 2

    Sorry. I did not understand that the American format. You just have the wrong grouping.

     (?:[0-2]\d)|(?:3[01])\.(?:0\d)|(?:1[012])\.(\d{1,4}) 
    • in this case, remains "xxxxxxxxx ..12 xx" - Alangel
    • @knes is not long regular?) --------------- /\d+.+(.*)/ - Palmervan
    • No, not long. Your does not take into account the fact that not only the date can be written through a dot. :) - knes
    • Alangel, use forward lookup. As an argument, this is the most regular in brackets. . * (? = (?: [0-2] \ d) | (?: 3 [01]) \. (?: 0 \ d) | (?: 1 [012]) \. (\ D {1 , 4})) Or something like that. - knes
    • preg_replace ("! (? = ([0-2] \ d | 3 [01]) \. (0 \ d | 1 [012]) \. (\ d {1,4})). +! is" , "", $ pass_agensy); it turned out something like this .. - Alangel

    And if without a regular expression? You can take part of your line before the first space:

     <?php $st = 'xxxxxxxxx 10.12.2012 xxxxxx'; $st = substr($st, 0, strpos($st, ' ')); // $st = 'xxxxxxxxx' ?> 
    • there is not necessarily one word, maybe a few - Alangel
    • Well, you can first find the position of the occurrence of the number and then as in the answer - zloctb
    • But let's say there are numbers in the string besides our date? - Alangel
    • Well, the date has a stable format? You can find a date through a regular. - zloctb
    • date either 12/10/2012 or 12/10/12 - Alangel