1

if is present またはif is presentをpreg_replace置き換える方法はありますか?ABABAB

何かのようなもの:

preg_replace('/ORDER BY field (ASC|DESC)/', 'ORDER BY field (***the alternative not matched***)');

ORDER BY field ASCanyをに変換しORDER BY field DESC、 any を に変換ORDER BY field DESCORDER BY field ASCます。

4

1 に答える 1

6

探している関数は preg_replace_callback です。

例:

function replace_asc_desc($matches)
{
    return 'ORDER BY field ' . ($matches[1] == 'ASC' ? 'DESC' : 'ASC');
}

$string = 'ORDER BY field DESC';

echo preg_replace_callback('/ORDER BY field (ASC|DESC)/', 'replace_asc_desc', $string);
于 2013-01-02T10:48:58.920 に答える