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 回答
0
$text = preg_replace('/\?(\w+)/', '<span class="$1">$1</span>', $text);
于 2013-10-06T20:13:27.247 回答