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

The key things here are:

  • Make sure you quote your company names before they go in to the regex, because you'll run in to problems if your company names contain characters that mean something in regex syntax
  • Use word boundaries (\b) to identify strings that are "on their own"
  • Wrap your company name in parenthesis in the regex, then you can access the parenthesis'd bit as $1 in the replacement if you need to

Consider the following example:

$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);

Which will give you:

{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 开发人员,但您应该使用 regex: "\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|AAPL}的字符串{COMPANY|BAX} {COMPANY|BAX}在第一和第二名 mybaxters,最后是 {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 回答