Php 如何在字符串的中间替换开始、结束或(作为一个单词)的公司名称?

Php 如何在字符串的中间替换开始、结束或(作为一个单词)的公司名称?,php,regex,Php,Regex,考虑以下数组: $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 我使用以下循环

考虑以下数组:

$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);
}
但是,我还想在字符串开头加上公司名称:

{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}
换句话说:我想替换公司名称的所有实例 -当被空间包围时,苹果是可爱的水果 -当在字符串的开头加上一个空格后,苹果就妙不可言了 -或者当在一个带前导空格的字符串的末尾时,这就是我的苹果

这可能需要一个正则表达式,但我在编写它时需要一些帮助。

试试这个:

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

}我想你需要的是带有单词边界的正则表达式\b


我不是php开发人员,但您应该使用正则表达式:\b+$name+\b。

这里的关键内容是:

确保在公司名称进入正则表达式之前引用它们,因为如果公司名称中包含的字符在正则表达式语法中具有某种含义,则会遇到问题 使用单词边界\b标识独立的字符串 在正则表达式中用括号括起您的公司名称,然后如果需要,您可以在替换中访问括号d位$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}

花了我一些时间,但你得到了一些东西

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

可能是重复的,他不想更换。b{COMPANY | AAPL}是错的,现在你是对的。即使这个新答案已经发布了,至少你提供了一些代码:谢谢!最后,如果在not之后加上空格:在允许但不是强制性的公司名称之前,正则表达式是什么?这是bapple和apples,这是bapple和{COMPANY | AA}s?@Pr0no你是指示例中的字符s还是空格?编辑正则表达式:感谢您的回复-不过还有一个额外的功能:如果在not:之后加上空格,正则表达式将是什么?在公司名称之前是允许的,但不是强制的?这就是bapple和苹果,这就是bapple和{COMPANY | AA}s?,这取决于你以后会看到什么。我怀疑你会在苹果公司的背景下看到苹果公司,也许是苹果的?。如果是这样,你可以试试/\b%s’?s?\b/你是对的,我的建议没有多大意义,你对撇号的建议是合乎逻辑的,但是在我要处理的字符串中,所有标点符号都被删除了,所以苹果的变成了苹果。不,我想是这样的:正则表达式应该允许公司名称+s,后面没有其他内容。因此,苹果公司、苹果公司和苹果公司将成为{COMPANY | AA}{COMPANY | AA}和苹果公司和苹果公司{COMPANY | AA}。那可能吗?我不害怕错误的替换,因为字符串的上下文都是技术方面的新闻:-我在前面的评论中的示例确实考虑到了这一点,是吗?'后面的表示撇号是可选的
{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}
$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;