RazvanZ
RazvanZ

Reputation: 218

PHP preg_replace() pattern

I have about 500 lines stored in a text file. Each line look like Filename_662344.xlsx , 324.

I would like to count numbers at the end of each line (324 in this case).

My idea is to delete part of lines using preg_replace() function. I tried preg_replace("/[^0-9]/", '', $line); , but the result was 662344324

How should the pattern look like if I want to delete the filename (including numbers);

Thanks!

Upvotes: 0

Views: 56

Answers (2)

MH2K9
MH2K9

Reputation: 12039

Can try this regex.

$line = 'Filename_662344.xlsx , 324';
$line = preg_replace("/^(.*?), /", '', $line);
echo $line;

Regex Demo

Upvotes: 2

Bijan
Bijan

Reputation: 8680

\d+$ will match the numbers at the end of a string

See Here for an example of it.

If you are trying to remove everything except the numbers, you can just use (^.*, ). This starts at the beginning of the line and select everything up to the comma. See Here for an example

Upvotes: 1

Related Questions