可能な値が既にテキストに含まれているテキストがあります。状況に応じて適切な値を表示したいと考えています。私は正規表現が苦手で、自分の問題を説明する方法がよくわからないので、ここに例を示します。私はそれをほとんど機能させました:
$string = "This [was a|is the] test!";
preg_replace('/\[(.*)\|(.*)\]/', '$1', $string);
// results in "This was a text!"
preg_replace('/\[(.*)\|(.*)\]/', '$2', $string);
// results in "This is the test!"
これは問題なく動作しますが、パーツが 2 つある場合は、最後からエンド ブラケットを取得するため、動作しなくなります。
$string = "This [was a|is the] so this is [bullshit|filler] text";
preg_replace('/\[(.*)\|(.*)\]/', '$1', $string);
//results in "This was a|is the] test so this is [bullshit text"
preg_replace('/\[(.*)\|(.*)\]/', '$2', $string);
//results in "This filler text"
状況 1 は ( と | の間の値である必要があり、状況 2 は | と ) の間の値を示す必要があります。