-4

I want to extract some words from a string.

The 24,000th eligible entrant will win. This giveaway started September 14,  
2018 3:35 PM PDT and ends the earlier of September 21, 2018 11:59 PM PDT or  
when the prize has been awarded.

The words I would like extracting are:

  • 24,000th
  • September 14, 2018 3:35 PM
  • September 21, 2018 11:59 PM

I have tried using the following:

$regex = "[\d,]+th|\w+[\d\s,:]+PM";

if (preg_match($regex, $str, $match)) {
    echo $match[0];
}
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
  • I think you shoud use the html code to do that. But please describe your question more. – Azhy Sep 15 '18 at 11:19
  • Yes i tried my best. I am here for somehelp – Mujahid Islam Sep 15 '18 at 11:20
  • Then show what you tried. – Azhy Sep 15 '18 at 11:20
  • I have a paragraph and i want to extract some words from it. You can see in my question – Mujahid Islam Sep 15 '18 at 11:21
  • you may try this: `[\d,]+th|September[\d\s,:]+PM` – The Scientific Method Sep 15 '18 at 11:23
  • The 24,000th eligible entrant will win. This giveaway started September 14, 2018 3:35 PM PDT and ends the earlier of September 21, 2018 11:59 PM PDT or when the prize has been awarded. $regex = '/[0-9][^\\.;]*(th)[^\\.;]*/'; if (preg_match($regex, $str, $match)) echo $match[0]; – Mujahid Islam Sep 15 '18 at 11:24
  • Please don't write code in comments. It's unreadable. Edit your question to include all relevant information/code. – M. Eriksson Sep 15 '18 at 11:32
  • 1
    Use [preg_match_all](http://php.net/manual/en/function.preg-match-all.php) with [delimiters](http://php.net/manual/en/regexp.reference.delimiters.php) for the regex: `$str = "The 24,000th eligible entrant will win. This giveaway started September 14, 2018 3:35 PM PDT and ends the earlier of September 21, 2018 11:59 PM PDT or when the prize has been awarded."; $regex = '~[\d,]+th|\w+[\d\s,:]+PM~'; preg_match_all($regex, $str, $matches); var_dump($matches);` – The fourth bird Sep 15 '18 at 11:44
  • @MujahidIslam I have just edited your question. Please see revision. What I have noticed is that you do not understand what a Regular Expression is. A regular expression needs to start and end with a `/`. And various flags can follow the trailing `/` – JustCarty Sep 15 '18 at 11:55

2 Answers2

1

you may try this

/[\d,]+th|September[\d\s,:]+PM/

if the month is changing

/[\d,]+th|\w+[\d\s,:]+PM/

in your code:

$regex = "/[\d,]+th|\w+[\d\s,:]+PM/"

if (preg_match_all($regex, $str, $match))
    echo $match[0]

;

for explanation see link

demo here

The Scientific Method
  • 2,374
  • 2
  • 14
  • 25
0

You need to add delimiter to your regex, and use preg_match_all:

$str = "The 24,000th eligible entrant will win. This giveaway started September 14, 2018 3:35 PM PDT and ends the earlier of September 21, 2018 11:59 PM PDT or when the prize has been awarded.";
$regex = "/[\d,]+th|\w+[\d\s,:]+PM/";

if (preg_match_all($regex, $str, $match))
    print_r($match[0]);

Output:

Array
(
    [0] => 24,000th
    [1] => September 14, 2018 3:35 PM
    [2] => September 21, 2018 11:59 PM
)
Toto
  • 89,455
  • 62
  • 89
  • 125