粗体关键字 with/without 字符串中的特殊字符 PHP

Bold keywords with/without special character in string PHP

$string = 'An aim is a goal or objective Hotel in Canada to achieve in life. In order to succeed in life, one must have Hotel in, Canada a goal. My aim in life is to be a teacher. Teaching is a noble and responsible profession. I have come City Hotels to know that the ever-increasing misery and distress, are City, Hotels due to the ignorance and illiteracy City. Hotels of the people of our country. So I have decided to spread education among the masses Hotel. in Canada as much as possible within my humble power. As a teacher, I shall try my best to impart man-making education.';

$keywords = ['City Hotels', 'Hotel in Canada'];

我想要的是,

$string = 'An aim is a goal or objective **Hotel in Canada** to achieve in life. In order to succeed in life, one must have **Hotel in, Canada** a goal. My aim in life is to be a teacher. Teaching is a noble and responsible profession. I have come **City Hotels** to know that the ever-increasing misery and distress, are **City, Hotels** due to the ignorance and illiteracy **City. Hotels** of the people of our country. So I have decided to spread education among the masses **Hotel. in Canada** as much as possible within my humble power. As a teacher, I shall try my best to impart man-making education.';

我做了什么,

foreach ($keywords as $keyword) {
  $normalKeyword = $keyword;
  $underlinedKeyword = '<b>'.$keyword.'</b>';
  $string = str_replace($normalKeyword, $underlinedKeyword, $string);
}

但我目前的输出是,

$string = 'An aim is a goal or objective **Hotel in Canada** to achieve in life. In order to succeed in life, one must have Hotel in, Canada a goal. My aim in life is to be a teacher. Teaching is a noble and responsible profession. I have come **City Hotels** to know that the ever-increasing misery and distress, are City, Hotels due to the ignorance and illiteracy City. Hotels of the people of our country. So I have decided to spread education among the masses Hotel. in Canada as much as possible within my humble power. As a teacher, I shall try my best to impart man-making education.';

那么如何在输入字符串中用一些特殊字符(例如:,.)加粗关键字

为了使 适应您的要求,遍历关键字数组,通过用一个或多个列入白名单的字符替换一个或多个空格来为正则表达式引擎准备关键字。为确保您进行 full-word 匹配,请使用单词边界 (\b).

代码:(Demo)

$string = 'An aim is a goal or objective Hotel in Canada to achieve in life. In order to succeed in life, one must have Hotel in, Canada a goal. My aim in life is to be a teacher. Teaching is a noble and responsible profession. I have come City Hotels to know that the ever-increasing misery and distress, are City, Hotels due to the ignorance and illiteracy City. Hotels of the people of our country. So I have decided to spread education among the masses Hotel. in Canada as much as possible within my humble power. As a teacher, I shall try my best to impart man-making education.';

$keywords = ['City Hotels', 'Hotel in Canada'];

foreach ($keywords as $keyword) {
    $regex = preg_replace('/ +/', '[.,\s]+', preg_quote($keyword, '/'));
    $string = preg_replace("/\b$regex\b/iu", '**[=10=]**', $string);
}
var_export($string);

输出:

'An aim is a goal or objective **Hotel in Canada** to achieve in life. In order to succeed in life, one must have **Hotel in, Canada** a goal. My aim in life is to be a teacher. Teaching is a noble and responsible profession. I have come **City Hotels** to know that the ever-increasing misery and distress, are **City, Hotels** due to the ignorance and illiteracy **City. Hotels** of the people of our country. So I have decided to spread education among the masses **Hotel. in Canada** as much as possible within my humble power. As a teacher, I shall try my best to impart man-making education.'