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 に答える
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 に答える