0

我在 PHP 中有一个字符串,其中包含一些我想更改的字符:例如,这是一段字符串:

$string = '***ROOMS*** The rooms and bathrooms were fully renovated in 2006. They are reported to be quite small but are very clean, well maintained and with modern bathrooms. Rooms are tastefully designed with warm colours and pine wooden furniture. ***RESTAURANTS & BARS*** There is no restaurant in the hotel however there the comfortable ground floor lounge is open all day where ';

我想像这样打印这一段:

<b>ROOMS</b><br>
 The rooms and bathrooms were fully renovated in 2006. They are reported to be quite small but are very clean, well maintained and with modern bathrooms. Rooms are tastefully designed with warm colours and pine wooden furniture.<br>
<b>RESTAURANTS & BARS</b><br> 
There is no restaurant in the hotel however there the comfortable ground floor lounge is open all day where 

这意味着 和 之间的字符串***变为***<br><b> string </b><br>

是否存在使用 str_replace 或模式来做到这一点的方法?

谢谢

4

4 回答 4

4

尝试这个 :

$string = '***ROOMS*** The rooms and bathrooms were fully renovated in 2006. They are reported to be quite small but are very clean, well maintained and with modern bathrooms. Rooms are tastefully designed with warm colours and pine wooden furniture. ***RESTAURANTS & BARS*** There is no restaurant in the hotel however there the comfortable ground floor lounge is open all day where ';
echo preg_replace("/\*\*\*([A-Za-z\& ]*)\*\*\*/", '<br><b>$1</b><br>', $string);

更新 :echo preg_replace("/\*{3}([^*]*)\*{3}/", '<br><b>$1</b><br>', $string);

于 2013-07-22T13:15:05.630 回答
3
preg_replace("/\*{3}(.*)\*{3}/Usi", "<br><b>\\1</b><br>", $text);
于 2013-07-22T13:15:58.150 回答
2

您将需要使用正则表达式来完成此操作。

就像是:

$newString = preg_replace('/\*\*\*([^*]+)\*\*\*/','<br/><b>$1</b><br/>',$string);

这将捕获一对***.

于 2013-07-22T13:16:54.173 回答
0
function doReplace($string)
    {
        //You can add to the following array
        //for multiple items to find within
        //the string
        $find    = array('/\*\*\*(.*?)\*\*\*/',
                         '/\*(.*?)\*/');

        //Set the replacement for each item above.
        //Make sure all the replacements are in the
        //same order for the items your finding.
        $replace = array('<b>$1</b>',
                         '<i>$1</i>');

        //Finally, do the replacement.
        return preg_replace($find, $replace, $string);
    }

    $string = '***ROOMS*** The *rooms* and bathrooms were fully renovated in 2006. They are reported to be quite small but are very clean, well maintained and with modern bathrooms. Rooms are tastefully designed with warm colours and pine wooden furniture. ***RESTAURANTS & BARS*** There is no restaurant in the hotel however there the comfortable ground floor lounge is open all day where ';

    echo doReplace($string);

如果您决定多次替换,上述功能会为您工作,只需添加到它来做这样的事情。

于 2013-07-22T13:27:11.767 回答