【问题标题】:Find and wrap SPAN around all selected words in a DIV在 DIV 中查找并环绕所有选定单词的 SPAN
【发布时间】:2018-03-18 13:30:50
【问题描述】:

我有一些预定义的单词,我想在一个句子中找到这些单词并添加一个 SPAN 标签。

例如;

Lorem Ipsum 只是打印和排版的虚拟文本 行业。 Lorem Ipsum 一直是业界标准的虚拟文本 自 1500 年代以来,当一位不知名的印刷商采用了一种类型的厨房和 争先恐后地制作了一本样书。

在这句话中,我想在单词中添加SPAM 标签:

单词:

  • 行业标准
  • 1500s
  • 标本书

DOM会是这样的

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

代码:

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

【问题讨论】:

  • 欢迎来到 StackOverflow!如果您遇到特定编程问题,我们很乐意为您提供帮助,但我们不是来为您编写代码或设计您的系统的。您至少需要尝试解决您自己的问题。请参阅How do I ask a good question?What topics can I ask about here?
  • @AlexHowansky 我更新了我的问题。你真的很快发表评论:)
  • 你在正确的轨道上。第一个提示——不要使用preg_replace(),使用str_replace()
  • 您的代码运行良好,请查看@AlexHowansky 处理所有单词的建议。
  • @AlexHowansky 我用了这个str_replace() 但没有任何改变我看不到我的错误

标签: javascript php jquery html regex


【解决方案1】:

当您不必使用正则表达式时,切勿使用正则表达式。 str_replace() 很适合你。有很多方法可以得到你想要的东西。最明显的是每次替换调用一次str_replace(),不断更新相同的输出字符串:

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

如果你想变得花哨,可以利用str_replace() 数组功能,一键搞定:

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

【讨论】:

  • 如何让大小字符不重要?
  • str_ireplace
  • @AlexHowansky 谢谢str_ireplace(array( "", "", "")); 是真的吗?如果属实,对我不起作用
  • @AlexHowansky - 好吧,我做到了 :) 再次感谢 Alex
【解决方案2】:

您需要将正则表达式模式(包括捕获组)放入数组的每个项目中。您现在的方式是尝试将数组注入字符串,这可能会产生“数组到字符串转换”错误。

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

我不能 100% 确定这是否是实现这一目标的最有效方法,但它确实可以得到你想要的(我假设你可以自己添加外部 &lt;div&gt; 标签)

DEMO

或者,这是一个使用 str_replace()array_walk() 的版本(这个想法归功于 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

【讨论】:

  • 如果数据库是这样注册的:industry\'s standard - 那么,这个过程将不起作用。不是吗?
  • @J.Doe 你可以运行$inputstripslashes() 来处理这个问题。你也必须用几乎任何其他方法来做同样的事情。您可能想先了解为什么要将斜杠存储在数据库记录中。 $output = preg_replace($replaceText, "$wrapBefore$1$wrapAfter", stripslashes($input));
  • 如何让大小字符不重要?
猜你喜欢
  • 2017-09-10
  • 2019-07-16
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多