1

I need to extract the highlighted numbers from each line in a CSV file.

Currently I am looping though the lines & splitting the line on the / character as this only appears once in each row, but how do I remove everything around these numbers so I am left with:

9/10

10/11

11/12

...

enter image description here

1
  • Why don't you try fgetcsv() with ~ as the delimiter? Commented Feb 6, 2019 at 19:37

1 Answer 1

4

If you want to only get the numbers, you could do a preg_match

$re = '/(\d+\/\d+)/s';
$str = 'dfsadsfadsfads~~9/10~~lfkjdskfds';

preg_match($re, $str, $matches);

but if you are able to get the entire doc as a single string, you could do a preg_match_all

$re = '/(\d+\/\d+)/s';
$str = 'dfsadsfadsfads~~9/10~~lfkjdskfds\ndfsadsfadsfads~~9/10~~lfkjdskfds\n';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

then loop on the $matches

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but seems to be stripping the left number to a single digit 9/10 0/11 1/12 3/14 - Also these numbers can be much larger than double digits
I would just add that you don't really need the m flag here. It's only relevant to the start and end of a string such as the use of ^ or $. Basically it changes those so they match the start and end of a line, instead of the whole string (the default behavior). It doesn't hurt anything to have it, just thought I would mention it. Otherwise, it's what I would have done.
@kb I fixed the left number issue by adding a + symbol to the regex. Should work like needed.
@ArtisticPhoenix I removed the m flag as suggested
@rtrigoso - Kool, I already upvoted you so I would do it again but alas I cant :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.