user8695752
user8695752

Reputation:

Find and wrap SPAN around all selected words in a DIV

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:

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

Upvotes: 0

Views: 335

Answers (2)

Alex Howansky
Alex Howansky

Reputation: 53563

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

Upvotes: 1

Patrick Q
Patrick Q

Reputation: 6393

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

Upvotes: 0

Related Questions