-1

現在、Twitter フィードを自分の Web サイトに取り込み、コンテンツをフロントページに表示しています。私ができるようにしたいのは、ハッシュタグまたは Twitter のユーザー名であるコンテンツをリンクに置き換えることだけです。

preg_replace を使用してこれを実行しようとしましたが、一致したパターンを参照して挿入する方法が不明であったため、置換として使用するリンクの作成に問題がありました。これが私がこれまでに得たものです(未完成)。誰でも私を助けることができますか?

ありがとう!

<?php 
foreach($tweets as $tweet) { ?>
  <?php 
    $pattern = '@([A-Za-z0-9_]+)';
    $replacement = "<a href=''>" . . "</a>";
    $regex_text = preg_replace($pattern, );

  ?>
  <div class="tweet2">
    <img src="images/quotes.png" />
    <p><?php echo $tweet[text]; ?></p>
  </div>
<?php }
?>
4

2 に答える 2

3
$regex_text = preg_replace($pattern, $replacement, $input_text);

これが preg_replace を使用する正しい方法$input_textです。置換元のテキストを含む変数です。

それはさておき:

$pattern="/@([A-Za-z0-9_]+)/"; //can't be sure if this will work w/o an example of a input string.
$replacement= "<a href=''>$1</a>";  //$1 is what you capture between `()` in the pattern.
于 2013-03-21T16:23:34.863 に答える