0

I have a string with long HTML-content like:

<TABLE cellPadding=3 width=100%><td><font class=btime>20:53</font>&nbsp;&nbsp;<a href=news2.php?st=1414691580 target=right>Кыргызстан: понастроили мечетей.., - Омор Рысбаев</a><br><font class=btime>20:50</font>&nbsp;&nbsp;<a href=news2.php?st=1414691400 target=right>Исламская Изида: как джихадисты вынуждают западные компании менять названия</a><br><font class=btime>20:48</font>&nbsp;&nbsp;<a href=news2.php?st=1414691280 target=right>Не прошло и двух лет. Российский летчик Петренко освобожден из плена талибов в Афганистане</a><br><font class=btime>19:58</font>&nbsp;&nbsp;<a href=news2.php?st=1414688280 target=right>Игорь Стрелков объявил о создании общественного движения "Новороссия"</a><br><font class=btime>19:27</font>&nbsp;&nbsp;<a href=news2.php?st=1414686420 target=right>Давление США на Россию будет только нарастать. Зачем Путин дал слабину? - Гао Фэн</a><br><font class=btime>19:21</font>&nbsp;&nbsp;<a href=news2.php?st=1414686060 target=right>Путин не знает страны, в которой живет. Тайга давным-давно полыхает.., - Э.Ханымамедов</a><br><font class=btime>19:11</font>&nbsp;&nbsp;<a href=news2.php?st=1414685460 target=right>Протестующие в Буркина-Фасо подожгли парламент, пытаясь свернуть бессменного (с 1987) президента Блэза Компаоре</a><br><font class=btime>18:37</font>&nbsp;&nbsp;<a href=news2.php?st=1414683420

from which I am trying to extract all news ids and write them in array. As you can see all the ids are placed between "?st=" and " target" markers. Would appreciate much for advising the best method to solve this task. Thank you.

georg
  • 211,518
  • 52
  • 313
  • 390
Sasha
  • 171
  • 1
  • 7
  • `(?<=\?st).*?(?=target)` this hsould so it – vks Oct 31 '14 at 07:05
  • 1
    The best method is to use an html parser. – georg Oct 31 '14 at 07:22
  • @georg, if html parser method is quicker than preg_match_all then I would switch the code and the accepted answer. Can you please give a specific solution with html parser to the above example? – Sasha Oct 31 '14 at 07:32
  • @Sasha: http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – georg Oct 31 '14 at 07:51
  • I will spend too much time to figure out how to use html parser to solve this exact task, that's why I asked specific example solution. Anyway, Avinash Raj's solution does fine for now. – Sasha Oct 31 '14 at 08:20

1 Answers1

0

Use the below regex in preg_match_all function to get an array of all the id's

\?st=\K\S+(?=\s*target|$)

DEMO

If you don't want to fetch the id which isn't followed by the string target (last id) then remove the |$ from the above regex.

\?st=\K\S+(?=\s*target)
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274