0
votes

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);
2
Welcome to StackOverflow! We will be glad to help you if you get stuck on a specific programming problem, but we are not here to write code or design your system for you. You will need to at least make an attempt at solving your own issue. Please see How do I ask a good question? and What topics can I ask about here?. - Alex Howansky
@AlexHowansky I updated my question. You really fast for comment :) - user8695752
You're on the right track. First hint -- don't use preg_replace() use str_replace(). - Alex Howansky
Your code works fine, look at @AlexHowansky `s suggestion to process all the words. - Nic3500
@AlexHowansky I used this str_replace() but nothing has changed I can not see the my mistake - user8695752

2 Answers

1
votes

Never use regex when you don't have to use regex. str_replace() will suit you just fine. There a quite a few ways to get what you're after. The most obvious is simply to call str_replace() once for each replacement, continually updating the same output string:

$out = $in;
foreach ($words as $word) {
    $out = str_replace($word, '<pre>' . $word . '<post>', $out);
}

If you want to get fancy, you can take advantage of the str_replace() array feature, and do it all in one shot:

$out = str_replace(
    $words,
    array_map(function($word){ return '<pre>' . $word . '<post>'; }, $words),
    $in
);
0
votes

You need to put the regex pattern (including the capturing group) in each item of the array. The way you have it now, you are trying to inject an array into a string, which is likely producing an "Array to string conversion" error.

$input = "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.";

$replaceText = array("/(industry's standard)/", "/(1500s)/", "/(specimen book)/");
$wrapBefore = '<span id="marked">';
$wrapAfter = '</span>';

$output = preg_replace($replaceText, "$wrapBefore$1$wrapAfter", $input);

echo $output;

I'm not 100% sure if this is the most efficient way to accomplish this, but it does get what you want (you can add the outer <div> tags yourself I assume)

DEMO

Alternatively, here's a version using str_replace() and array_walk() (credit for the idea goes to Alex)

$input = "Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.. to make a type specimen book.";

$searchArray = array("industry's standard", "1500s", "specimen book");
$replacementArray = $searchArray;

$wrapBefore = '<span id="marked">';
$wrapAfter = '</span>';
array_walk($replacementArray, "addWrappers", array($wrapBefore, $wrapAfter));

$output = str_replace($searchArray, $replacementArray, stripslashes($input));

echo $output;

function addWrappers(&$searchTerm, $key, $additionalText)
{
    $searchTerm = $additionalText[0] . $searchTerm . $additionalText[1];
}

DEMO