/ / Phasing hashtag in PHP API PHP - php, regex, twitter, hashtag

Phasing hashtag in PHP API PHP - php, regex, twitter, hashtag

Voglio analizzare gli hashtag dai tweet che sto recuperando da Twitter. Ora, non ho trovato nulla disponibile nell'API. Quindi, lo sto analizzando da solo usando php, ho provato diverse cose.

<?php
$subject = "This is a simple #hashtag";
$pattern = "#S*w";
preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

Ho anche provato

$pattern = "/[#]"."[A-Za-z0-9-_]"."/g";

Ma poi mostra / g non è riconosciuto da php. Ho cercato di farlo da un po 'di tempo ma non riesco a farlo. Quindi per favore aiuto.

Post scriptum : Ho poche idee su Experssions regolari.

risposte:

1 per risposta № 1

Devi considerare dove potrebbe apparire un hashtag. Ci sono tre casi:

  • all'inizio di un tweet,
  • dopo lo spazio bianco,
  • nel mezzo di una parola - questo non deve essere considerato come un hashtag.

Quindi questo li abbinerà correttamente:

"/(^|s)#w+/"

Spiegazione:

  • ^ può essere usato in O dichiarazioni
  • s è usato per catturare spazi, tabulazioni e nuove linee

Ecco il codice completo:

<?php
$subject = "#hashtag This is a simple #hashtag hello world #hastag2 last string not-a-hash-tag#hashtag3 and yet not -#hashtag";
$pattern = "/(?:^|s)(#w+)/";
preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

0 per risposta № 2

Questo funziona per me:

$subject = "This is a simple #hashtag hello world #hastag2 last string #hashtag3";
$pattern = "/(#w+)/";
preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);