2016-01-06 65 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]

相关问题