0

私がやりたいのは、次のような文字列をフィルタリングすることです:

"?group This is a title"

そして、次のようなものを出力します。

<span class="group">group</span> This is a title

私の質問はもちろん、PHPでこれは可能ですか? そして、どうすればこのようなことができますか?

4

3 に答える 3

2

これを試して

$text = preg_replace('/\?(\w+)/', '<span class="$1">$1</span>', $text);
于 2013-10-06T20:31:35.100 に答える
0

これはあなたが必要とするものです:

$string = "wordt bla shit happends and this wil be ?fat and ?this";

$array = array();

// Wanna search on other characters change the second ? and not the first. The first ? is part of the syntax.
preg_match_all('/(?<!\w)\?\w+/', $string, $array);

$result = $array[0];
foreach($result as $value){
    $word = ltrim($value,'?');
    // in your case change the html to whatever you want
    $string = str_replace($value,"<b>$word</b>",$string);
}

echo $string;

結果は次のようになります。

wordt bla shit happends and this wil be <b>fat</b> and <b>this</b>
于 2013-10-06T20:31:23.743 に答える