4

私はこれを持っています:

$only_left_pattern = '/[^-{]\bleft\b/';
$subject = 'body {left: 24px;}\n p {right:30px;}';
$subject = preg_replace($only_left_pattern, 'right', $subject);
echo $subject;

一致させたい文字列ごとにパターンを宣言する必要があります。右または左を一致させて、それに応じて置き換える方法があれば?例(私の頭からの構文):

$right_left_pattern = '/[^-{](\bleft\b|\bright\b/)';

// if you found left replace with right, if you found right replace with left
// acrording to the order in the pattern
$subject = preg_replace($only_left_pattern, right|left, $subject);

私の例のようなことを PHP や正規表現だけで行うことができるかどうかを尋ねています。

私はデイブの解決策を試しました

$pattern = array('/[^-{]\bleft\b/','/[^-{]\bright\b/');
$replace = array('right','left');
$subject = 'body {left: 24px;} p {right: 24px;}';
$subject = preg_replace($pattern, $replace, $subject);
echo $subject;

regexしかし、それは機能しません。配列としてではなくテストしましたが、機能しています。

4

1 に答える 1