1

さて、ヘッダーの質問は混乱を招くように聞こえるかもしれません。ええ、私も混乱していました。とにかく、私が欲しかったのはこれです: たとえば、このテキスト行があるとします。

The quick brown @fox jumps @over the @lazy dog.

このテキスト行は、データベースから動的にフェッチされた「単一行」であり、テキストの配列ではありません。最初の文字が '@' のテキストがページまたは何かへのリンクであると仮定すると、アンカー タグを配置する場所を指定できるようになり、私の場合は、' で始まる各テキストにアンカー タグを配置したいと考えています。 @'.

私は爆発を試みましたが、爆発はこれに対する答えではないようです。誰かがここで私を助けてくれますか? ありがとう。

4

2 に答える 2

2

そのためには使用したくありませんexplodeが、正規表現です。複数の出現を一致させるにpreg_match_allは、お得です。

preg_match_all('/@\w+/', $input, $matches);

        #        @   is the literal "@" character
        #    and \w+ matches consecutive letters

preg_replaceそれらをリンクに変換するために使用することをお勧めします。またはpreg_replace_callback、一部のロジックをハンドラー関数に移動することをお勧めします。

于 2012-07-02T18:45:13.463 に答える
0

爆発を使用して、@ の前にある単語を処理できます...それは本当に何をしたいかによって異なります。

//Store the string in a variable
$textVar = "The quick brown @fox jumps @over the @lazy dog.";

//Use explode to separate words
$words = explode(" ", $textVar);

//Check all the variables in the array, if the first character is a @
//keep it, else, unset it
foreach($words as $key=>$val) {
    if(substr($val, 0, 1) != "@") {
        unset($words[$key]);
    } else {
        $words[$key] = "<a href='#'>".$words[$key]."</a>";
    }
}

//You can now printout the array and you will get only the words that start with @
foreach($words as $word) {
    echo $word."<br>";
}

@ を持たない文字列を保持し、内破を使用してすべてをまとめることもできます。

//Store the string in a variable
$textVar = "The quick brown @fox jumps @over the @lazy dog.";

//Use explode to separate words
$words = explode(" ", $textVar);

//Check all the variables in the array, if the first character is a @
//keep it, else, unset it
foreach($words as $key=>$val) {
    if(substr($val, 0, 1) != "@") {
        //Do nothing
    } else {
        $words[$key] = "<a href='#'>".$words[$key]."</a>";
    }
}

//You can now printout the string
$words = implode($words, " ");
echo $words;
于 2012-07-02T19:06:44.227 に答える