0

次の配列について考えてみます。

$companies = array(
  'apple' => 'AAPL',
  'baxter' => 'BAX'
);

そして、次の文字列:

apple at the beginning of string with bapple
here a string with apple in the middle
baxter baxter on first and second place mybaxters
and finally, baxter

次のループを使用して、会社名をそれぞれのティッカーに置き換えています。

foreach ($companies as $name => $ticker) {
  $tweet = str_replace(" $name", "<b>{COMPANY|$ticker}</b>", $tweet);
}

これにより、

apple at the beginning of string with bapple
here a string with {COMPANY|AAPL} in the middle
baxter {COMPANY|BAX} on first and second place mybaxters
and finally, {COMPANY|BAX}

ただし、文字列の先頭に会社名を追加したいと思います。

{COMPANY|AAPL} at the beginning of string with bapple
here a string with {COMPANY|AAPL} in the middle
{COMPANY|BAX} {COMPANY|BAX} on first and second place mybaxters
and finally, {COMPANY|BAX}

ただし、のスペースを削除すると" $name"、次のような単語bappleも置き換えられます。

{COMPANY|AAPL} at the beginning of string with b{COMPANY|AAPL}

言い換えると、会社名のすべてのインスタンスを、「リンゴは素敵な果物」というスペースで囲まれている場合、文字列の最初が「リンゴは素晴らしい」の後のスペースにある場合、または文字列の最後にある場合に置き換えたいと考えています。先頭に「これが私のリンゴです」

これにはおそらく正規表現が必要ですが、それを書くのに助けが必要です。

4

5 に答える 5

2

ここで重要なことは次のとおりです。

  • 会社名に正規表現構文で何かを意味する文字が含まれていると問題が発生するため、正規表現に入る前に必ず会社名を引用してください。
  • 単語の境界(\b)を使用して、「独自の」文字列を識別します
  • 正規表現の括弧で会社名をラップすると、 $1必要に応じて、置換の場合と同じように括弧で囲まれたビットにアクセスできます。

次の例を考えてみましょう。

$companies = array(
  'apple'   => 'AAPL',
  'baxter'  => 'BAX'
);

$input = "apple at the beginning of string with bapple
here a string with apple in the middle
baxter baxter on first and second place mybaxters
and finally, baxter";


foreach($companies as $name => $code)
{
  $input = preg_replace(sprintf('/\b(%s)\b/i',preg_quote($name)),'{COMPANY:'.$code.'}',$input);
}

var_dump($input);

それはあなたに与えるでしょう:

{COMPANY:AAPL} at the beginning of string with bapple
here a string with {COMPANY:AAPL} in the middle
{COMPANY:BAX} {COMPANY:BAX} on first and second place mybaxters
and finally, {COMPANY:BAX}
于 2012-04-24T16:31:49.987 に答える
2

必要なのは単語境界のある正規表現だと思います\b

http://www.regular-expressions.info/wordboundaries.html

于 2012-04-24T16:27:15.880 に答える
2

私はPHP開発者ではありませんが、正規表現を使用する必要があります"\b"+$name+"\b"

于 2012-04-24T16:27:39.047 に答える
1

しばらく時間がかかりましたが、何かを手に入れました

$companies = array(
    'apple' => 'AAPL',
    'baxter' => 'BAX'
);

$str = 'apple at the beginning of string with bapple
here a string with apple in the middle
baxter baxter on first and second place mybaxters
and finally, baxter';

foreach($companies as $search => $company)
{
    $regex = '!(?<=\b|^)('.$search.')(?=\b|$)!ui';

    $str = preg_replace($regex, $company, $str);
}

echo $str;
于 2012-04-24T16:42:12.523 に答える
1

これを試して:

foreach ($companies as $name => $ticker) {
  $tweet = preg_replace('/\b'.preg_quote($name).'\b/', "<b>{COMPANY|$ticker}</b>", $tweet);
}

正規表現は、いわゆる単語境界を使用します。http: //www.regular-expressions.info/wordboundaries.html


出力は次のようになります。

{COMPANY | AAPL}、bappleを使用した文字列の先頭にあり、中央に{COMPANY | BAX} {COMPANY | BAX}があり、最後に {COMPANY|BAX}があります。

のようなものもサポートしたい場合はapples、次のコードを使用してください。

foreach ($companies as $name => $ticker) {
  $tweet = preg_replace('/\b'.preg_quote($name).'s{0,1}\b/', "<b>{COMPANY|$ticker}</b>", $tweet);

}

于 2012-04-24T16:26:58.613 に答える