Preg Replace - 替换第二次出现的匹配项

发布于 2024-11-01 22:38:43 字数 968 浏览 2 评论 0原文

我对 php 比较陌生,希望有人可以帮助我替换正则表达式,或者可能是我不太确定的匹配替换。

我想自动将(第二次出现的匹配项)加粗,然后将第四次出现的匹配项设为斜体,然后将第七次出现的匹配项加下划线。

这基本上是出于内容搜索引擎优化的目的。

我已经做了一些替换: 并认为这应该可以解决问题?

preg_replace( pattern, replacement, subject [, limit ])

我已经知道我想用的词了 假设

'pattern' is also a word that is already defined like [word].
`replacement` 'This is a variable I am getting from a mysql db.
'subject' - The subject is text from a db.

我有这个内容: 这或多或少解释了我想做的事情。

这是我要替换的文本示例。在本文中,我想让单词 example < 再次出现。大胆的。然后我想跳过文本中下次出现的示例,并使第 4 次出现的单词 example 以斜体显示。然后我想跳过文本中第五次出现 example 这个词,以及第六次,最后想要第七次 example 出现在文本中并加下划线。在此示例中,我使用超链接作为下划线示例,因为我在文本编辑器中没有看到下划线功能。 example 这个词可能在文本中出现多次,但我唯一的要求是下划线一次,粗体一次,斜体一次。我稍后可能会决定对“示例”一词进行一些引用,但这还不是优先事项。

如果该单词至少出现 7 次,则代码不要出错,这一点也很重要。

我将如何做到这一点,任何想法将不胜感激。

I am relatively new to php, and hope someone can help me with a replace regex, or maybe a match replace I am not exactly sure.

I want to automatically bold the (second occurance of a match) and then make the 4th appearance of a match italic and then the 7th appearance of a match underlined.

This is basically for SEO purposes in content.

I have done some replacements with: and were thinking this should do the trick?

preg_replace( pattern, replacement, subject [, limit ])

I already know the word I want to use in

'pattern' is also a word that is already defined like [word].
`replacement` 'This is a variable I am getting from a mysql db.
'subject' - The subject is text from a db.

Lets say I have this content: This explains more or less what I want to do.

This is an example of the text that I want to replace. In this text I want to make the second occurance of the word example < bold. Then I want to skip the next time example occurs in the text, and make the 4th time the word example appears in italic. Then I want to skip the 5th time the word example appears in the text, as well as the 6th time and lastly wants to make the 7th time example appears in the text underline it. In this example I have used a hyperlink as the underline example as I do not see an underline function in the text editor. The word example may appear more times in the text, but my only requerement is to underline once, make bold once and make italic once. I may later descide to do some quotes on the word "example" as well but it is not yet priority.

It is also important for the code not to through an error if there is not atleast 7 occurances of the word.

How would I do this, any ideas would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情栀口红 2024-11-08 22:38:43

您可以使用 preg_split 在匹配处拆分文本,应用修改,然后将所有内容重新组合在一起:

$parts = preg_split('/(example)/', $str, 7, PREG_SPLIT_DELIM_CAPTURE);
if (isset($parts[3])) $parts[3] = '<b>'.$parts[3].'</b>';
if (isset($parts[7])) $parts[7] = '<i>'.$parts[7].'</i>';
if (isset($parts[13])) $parts[13] = '<u>'.$parts[13].'</u>';
$str = implode('', $parts);

第 i 个匹配的索引公式为 index = i · 2 - 1。

You could use preg_split to split the text at the matches, apply the modifications, and then put everything back together:

$parts = preg_split('/(example)/', $str, 7, PREG_SPLIT_DELIM_CAPTURE);
if (isset($parts[3])) $parts[3] = '<b>'.$parts[3].'</b>';
if (isset($parts[7])) $parts[7] = '<i>'.$parts[7].'</i>';
if (isset($parts[13])) $parts[13] = '<u>'.$parts[13].'</u>';
$str = implode('', $parts);

The index formula for the i-th match is index = i · 2 - 1.

∞梦里开花 2024-11-08 22:38:43

正则表达式本身无法计数,并且 preg_ 函数提供的帮助很少。你需要一个解决方法。如果您实际上只想搜索一个单词,您可能需要使用字符串函数。否则尝试:

// just counting
if (7 >= preg_match_all($pattern, $subject, $matches)) {

   $cb_num = 0;
   $subject = preg_replace_callback($pattern, "cb_ibu", $subject);
}

function cb_ibu($match) {
    global $cb_num;

    $match = $match[0];

    switch (++$cb_num) {
        case 2: return "<b>$match</b>";
        case 4: return "<i>$match</i>";
        case 7: return "<u>$match</u>";
        default: return $match;
    }
}

技巧是有一个回调来进行记帐。在那里添加任何规则都很容易。

The regular expression itself cannot count, and the preg_ functions provide little help. You need a workaround. If you were to actually search for just a word, you might want to use string functions. Otherwise try:

// just counting
if (7 >= preg_match_all($pattern, $subject, $matches)) {

   $cb_num = 0;
   $subject = preg_replace_callback($pattern, "cb_ibu", $subject);
}

function cb_ibu($match) {
    global $cb_num;

    $match = $match[0];

    switch (++$cb_num) {
        case 2: return "<b>$match</b>";
        case 4: return "<i>$match</i>";
        case 7: return "<u>$match</u>";
        default: return $match;
    }
}

The trick is to have a callback which does the accounting. And there it's quite easy to add any rules.

妄司 2024-11-08 22:38:43

这是一个有趣的问题。我的实现是:

function replace_exact($word, $tag, $string, $limit) {
    $tag1 = '<'.$tag.'>';
    $tag2 = '</'.$tag.'>';
    $string = str_replace($word, $tag1.$word.$tag2, $string, 1);
    if ($limit==1) return $string;
    return str_replace($tag1.$word.$tag2,$word,$string,$limit-1);
}

像这样使用它:

echo replace_exact('Example', 'b', $source_text, 2);
echo replace_exact('Example', 'i', $source_text, 4);

我不知道它的运行速度有多快,但它会比 preg_replace 更快。

That's an interesting question. My implementation would be:

function replace_exact($word, $tag, $string, $limit) {
    $tag1 = '<'.$tag.'>';
    $tag2 = '</'.$tag.'>';
    $string = str_replace($word, $tag1.$word.$tag2, $string, 1);
    if ($limit==1) return $string;
    return str_replace($tag1.$word.$tag2,$word,$string,$limit-1);
}

Use it like this:

echo replace_exact('Example', 'b', $source_text, 2);
echo replace_exact('Example', 'i', $source_text, 4);

I don't know about how fast this will work, but it will be faster than preg_replace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文