文字列でハッシュタグとそれに接続されている文字を確認するにはどうすればよいですか?
例:これは私のサンプル文字列#exampleです。
*これを抽出したい: * #example
次に、見つかった文字列のテキストの色を変更したい
これにより、ハッシュタグがスパン要素で囲まれます。
$sampleText = 'This is my sample string #example of what I mean. Here is another #test.';
$pattern = '/#[a-zA-Z0-9]*/';
$replacement = '<span class="otherColor">$0</span>';
$updatedText = preg_replace($pattern, $replacement ,$sampleText);
print_r($updatedText);
出力:
これは、私の言いたいことのサンプル文字列<span class = "otherColor"> #example </ span>です。これが別の<spanclass = "otherColor"> #test </ span>です。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加