私がやりたいのは、次のような文字列をフィルタリングすることです:
"?group This is a title"
そして、次のようなものを出力します。
<span class="group">group</span> This is a title
私の質問はもちろん、PHPでこれは可能ですか? そして、どうすればこのようなことができますか?
これを試して
$text = preg_replace('/\?(\w+)/', '<span class="$1">$1</span>', $text);
これはあなたが必要とするものです:
$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>