PHP的字符串标记生成器

PHP的字符串标记生成器

问题描述:

I have used the String Tokenizer in Java. I wish to know if there is similar functionality for PHP. I have a string and I want to extract individual words from it.

eg. If the string is -

Summer is doubtful #haiku #poetry #babel

I want to know if it contains the hashtag #haiku.

我在Java中使用了字符串标记符。 我想知道PHP是否有类似的功能。 我有一个字符串,我想从中提取单个单词。 p>

例如。 如果字符串是 - p>

 夏天是值得怀疑的#haiku #poetry #babel 
  code>  pre> 
 
 

我想知道是否 它包含主题标签 #haiku code>。 p> div>

strpos, stripos, strstr, stristr are easy solutions.

strpos example:

$haikuIndex = strpos( $str, '#haiku' ); 
if( $haikuIndex !== FALSE ) {
   // "#haiku" exists
}

strstr example:

$haikuExists = strstr( $str, '#haiku' );

if( $haikuExists !== FALSE ) {
   // "#haiku" exists
}

You can also use strstr

if (strlen(strstr($str,'#haiku')) > 0) // "#haiku" exists

If you want a string tokenizer, then you probably want the strtok function

<?php
$string = "Summer is doubtful #haiku #poetry #babel";
$tok = strtok($string, " ");
while ($tok !== false) {
    if ($tok == "#haiku") {
        // #haiku exists
    }
    $tok = strtok(" ");
}
?>