Home > Mobile >  Bold keywords with/without special character in string PHP
Bold keywords with/without special character in string PHP

Time:04-01

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

What I want is,

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

What I did,

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

But my current output is,

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

So how can i bold the keywords with some special characters(ex: ,.) in the input string

CodePudding user response:

To adapt Nick's similar solution for your requirements, loop through your array of keywords, prepare the keyword for the regex engine by replacing one or more spaces with one or more of your whitelisted characters. To ensure that you are making full-word matches, use word boundaries (\b).

Code: (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", '**$0**', $string);
}
var_export($string);

Output:

'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.'
  • Related