2016-01-06 110 views
0

我目前正在抓取在字符串中發現的所有#hashtags(即推文)。按原樣工作。檢查hashtag是否在字符串的開頭或中間php

但是,我想要查找僅位於字符串中間字符串OR(或足夠接近它)的字符串OR開頭的哈希標籤。換句話說,找到所有不在字符串末尾的哈希標籤。

獎勵積分,如果你還可以點我就怎麼看,如果一個主題標籤在字符串的結尾存在以及方向。

$tweet = 'This is an #example tweet'; 

preg_match_all('/(#\w+)/u', $tweet, $matches); 

if ($matches) { // found hashtag(s) } 
+0

幾個主題標籤?像[''/\G(#\w+)[^\w#]*/u''](https://regex101.com/r/sU1gR4/1)?或者只是在開始,結束,而不是在字符串的開始/結束時使用單個hashtag? –

+0

假設單詞和hastags總是由空格分隔? – AbraCadaver

+0

我認爲要找到「中等或足夠接近」,你必須找到長度並在該區域應用正則表達式。你期望在這個'這是一個#example #backxample tweet'; – zod

回答

1
// Check if Hashtag is last word; the strpos and explode way: 

$tweet = 'This is an #example #tweet'; 
$words = explode(" ", $tweet); 
$last_word = end($words); 

// count the number of times "#" occurs in the $tweet. 
// if # exists in somewhere else $exists_anywhere equals TRUE !! 
$exists_anywhere = substr_count($tweet,'#') > 1 ? TRUE : FALSE ; 

if(strpos($last_word,'#') !== FALSE ) { 
    // last word has # 
} 

從DOC:

不要使用的preg_match(),如果你只是想檢查一個字符串是否包含在另一個字符串 。使用strpos()或strstr(),因爲它們 會更快。

+0

謝謝,但'tweet#'不會是一個有效的標籤。我需要在最後尋找類似'#tweet'的東西...... –

+0

新代碼呢? – smoqadam

+0

'end()'可以像那樣爆炸嗎? –

-1

UPDATE/EDIT

$tweet = "Hello# It's #a# beaut#iful day. #tweet"; 
$tweet_arr = explode(" ", $tweet); 
$arrCount = 0; 

foreach ($tweet_arr as $word) { 
    $arrCount ++; 
    if (strpos($word, '#') !== false) { 
     $end = substr($word, -1); 
     $beginning = substr($word, 0, 1); 
     $middle_string = substr($word, 1, -1); 
     if ($beginning === "#") { 
      echo "hash is at the beginning on word " . $arrCount . "<br />"; 
     } 
     if (strpos($middle_string, '#') !== false) { 
      $charNum = strpos($middle_string, '#') + 1; 
      echo "hash is in the middle at character number " . $charNum . " on word " . $arrCount . "<br />"; 
     } 
     if ($end === "#") { 
      echo "hash is at the end on word " . $arrCount . "<br />"; 
     } 
    } 
} 
+0

我需要在最後尋找#tweet之類的東西......最後只會尋找#作爲最後一個字符嗎? –

+0

我添加了代碼...那是你在找什麼? – Zak

+0

呃,不,不完全。 '#tweet'只是一個例子。它可以是任何哈希標籤。 –

1

要匹配的僅僅是開始:

/^(#\w+)/ 

要查找特定#hashtag:

/^#tweet/ 

要匹配在中間的任何地方(不開頭或結尾):

/^[^#]+(#\w+)[^\w]+$/ 

要查找特定#hashtag:

/^[^#]+#tweet[^\w]$/ 

要匹配只在結尾:

/(#\w+)$/ 

爲了尋找一個特定的#hashtag:

/#tweet$/ 
+0

我需要在最後尋找類似'#tweet'的東西......最後只會尋找'#'作爲最後一個字符嗎? –

+0

已編輯。以爲你試圖在不同的位置找到未知單詞的標籤。 – AbraCadaver

+0

我是......'#tweet'只是一個例子;) –

0

行,我個人會變成串入詞的數組:

$words = explode(' ', $tweet); 

然後運行上的第一個字的檢查:

preg_match_all('/(#\w+)/u', $words[0], $matches); 
if ($matches) { 
    //first word has a hashtag 
} 

然後你就可以通過陣列的其餘部分只是走#標籤中中間。 最後檢查的最後一個字,

$sizeof = count($words) - 1; 
preg_match_all('/(#\w+)/u', $word[$sizeof], $matches); 
if ($matches) { 
    //last word has a hashtag 
} 
1
preg_match_all('/(?!#\w+\W+$)(#\w+)\W/', $tweet, $result); 

這是一個人們#tweet將趕上#tweet

#tweet人的#Second示例將捕獲#Second#tweet

#tweet的#Another示例將捕獲#Another但不是#tweet(即使它結束於!.或任何其他非單詞字符)

我們差不多完成了#yup!不會抓到任何東西

最後一個#tweet!晚安將趕上#tweet

當然,所有的hastags(捕獲)將在一開始就存儲在$result[1]

相關問題