I have some predefined words and I want to find these words in a sentence and add a SPAN tag.
For example;
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
In this sentence I would like to add a SPAM label to the words:
Words:
- industry's standard
- 1500s
- specimen book
DOM will be like this
<div class="exampleArticle">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has
been the <span id="marked">industry's standard</span> dummy text ever since the <span id="marked">1500s</span>, when an unknown
printer took a galley of type and scrambled it to make a type <span id="marked">specimen book</span>.
</div>
Code:
$in = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.';
$words = array(
"industry's standard", "1500s", "specimen book"
);
$wrap_before = '<span id="marked">';
$wrap_after = '</span>';
$out = preg_replace("/($words)/i", "$wrap_before$1$wrap_after", $in);
preg_replace()usestr_replace(). - Alex Howanskystr_replace()but nothing has changed I can not see the my mistake - user8695752