preg_replace效率

发布于 2024-12-08 15:35:36 字数 1289 浏览 1 评论 0原文

我使用 preg_replace 来实现流行语类型功能,使用流行语列表,在文章中搜索它们,然后使用 preg_replace 将链接包装在它们周围。

我的流行语是:

Online Marketing
Social Media Markeing
Social Media
Social
Brand Marketing
Brand
Search Engine Optimisation
SEO
Email Marketing
Email
Twitter
Facebook
LinkedIn

我正在像这样做我的 preg_replace...

$text = preg_replace("/Online Marketing/", "<a href='".$base."online-marketing/'>Online Marketing</a>", $text);

但问题是我必须为每个可能的变化做一个 preg_replace 就像这样...

$text = preg_replace("/Online Marketing/", "<a href='".$base."online-marketing/'>Online Marketing</a>", $text);
$text = preg_replace("/Online marketing/", "<a href='".$base."online-marketing/'>Online marketing</a>", $text);
$text = preg_replace("/online marketing/", "<a href='".$base."online-marketing/'>online marketing</a>", $text);

这是极其冗长的,现在我知道我可以使用数组与这样的 str_repalce :

$from = array("Online Marketing", "Online marketing", "online marketing");
$name = str_replace($from, "", $text);

但我看不到一种方法来查找数组的哪一部分匹配,然后将其替换为正确的链接,因为您可以看到上面的 str_replace 完全删除了我的流行语,这只是一个示例。

所以我想知道是否有人能够帮助我找到一种在 str_replace 或 preg_replace 中使用流行语数组的方法,同时确保它用正确的链接替换流行语。

希望有人可以帮忙

I am using preg_replace for buzzword type functionality, using a list of buzzwords, searching for them in an article and then using preg_replace to wrap a link around them.

My buzzwords are:

Online Marketing
Social Media Markeing
Social Media
Social
Brand Marketing
Brand
Search Engine Optimisation
SEO
Email Marketing
Email
Twitter
Facebook
LinkedIn

and I am doing my preg_replace like this...

$text = preg_replace("/Online Marketing/", "<a href='".$base."online-marketing/'>Online Marketing</a>", $text);

but the issue is I am having to do a preg_replace for each possible variation like so...

$text = preg_replace("/Online Marketing/", "<a href='".$base."online-marketing/'>Online Marketing</a>", $text);
$text = preg_replace("/Online marketing/", "<a href='".$base."online-marketing/'>Online marketing</a>", $text);
$text = preg_replace("/online marketing/", "<a href='".$base."online-marketing/'>online marketing</a>", $text);

Which is extremely long winded, now I know that I can use an array with str_repalce like this:

$from = array("Online Marketing", "Online marketing", "online marketing");
$name = str_replace($from, "", $text);

But I cannot see a way of finding which part of the array was matched and then replacing it with the correct link, as you can see the str_replace above just totally deletes my buzzword, its just an example.

So I was wondering if anyone would be able to help me find a way of using an array of buzzwords in a str_replace or preg_replace but also make sure that it repalces the buzzword with the correct link.

Hope someone can help

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

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

发布评论

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

评论(4

挥剑断情 2024-12-15 15:35:36
$test = preg_replace("/online marketing/i", "<a href='".$base."'online-marketing/'>$0</a>, $text);

或者,如果维持大写并不重要:

$test = str_ireplace("online marketing", "<a href='".$base."'online-marketing/'>online marketing</a>, $text);
$test = preg_replace("/online marketing/i", "<a href='".$base."'online-marketing/'>$0</a>, $text);

Or, if maintaining capitalisation is not important:

$test = str_ireplace("online marketing", "<a href='".$base."'online-marketing/'>online marketing</a>, $text);
野却迷人 2024-12-15 15:35:36

函数 preg_replace 可以接收第二个替换数组。
如果您打算始终使用相同的语法,则可以执行循环来生成链接(而不是每次想要拥有元素时自己编写)

$base = 'base/';
$from = array("Online Marketing", "Online test", "offline test");
$to = array();
foreach($from AS $key=>$f) {
   $from[$key] = "/".$f."/";    
   $to[$key]="<a href='".$base.$f."/'>online marketing</a>";
}
$string = 'this is a test using Online Marketing and offline test';
$text = preg_replace($from, $to, $string);

Function preg_replace can receive a second array of replacements.
If you are going to use always the same syntax, you can do a loop to generate the link (instead of writing it yourself each time you want to had an element)

$base = 'base/';
$from = array("Online Marketing", "Online test", "offline test");
$to = array();
foreach($from AS $key=>$f) {
   $from[$key] = "/".$f."/";    
   $to[$key]="<a href='".$base.$f."/'>online marketing</a>";
}
$string = 'this is a test using Online Marketing and offline test';
$text = preg_replace($from, $to, $string);
太阳公公是暖光 2024-12-15 15:35:36

您可以将 str_ireplace() 与数组一起使用(这是不区分大小写的匹配字符串替换),因此如果找到某个匹配项,它将用另一个数组中的相应替换项替换它。

例如:

$needles = array("Online Marketing", "Online Games", "Online Shopping");

$replacements = str_replace('<a href="online_marketing.html>Online Marketing</a>"',
                             '<a href="online_games.html">Online Games</a>',
                             '<a href="online_shopping.html">Online Shopping</a>');

    $replaced = str_ireplace($needles, $replacements);

    // Show the replaced text
    echo $replaced;
);

You can use str_ireplace() with arrays (which is a case insensitive match string replacement), so if a certain match is found, it will replace it with the corresponding replacement in the other array.

EG:

$needles = array("Online Marketing", "Online Games", "Online Shopping");

$replacements = str_replace('<a href="online_marketing.html>Online Marketing</a>"',
                             '<a href="online_games.html">Online Games</a>',
                             '<a href="online_shopping.html">Online Shopping</a>');

    $replaced = str_ireplace($needles, $replacements);

    // Show the replaced text
    echo $replaced;
);
十年不长 2024-12-15 15:35:36

我混合了你所有的答案,这是我的最终结果...

$Fbuzzwords = array(" online marketing", " social media marketing", " social-media-marketing", " social media", 
                " social", " brand marketing", " brand", " search engine optimisation", " seo", " email marketing", 
                " email", " e-mail marketing", " e-mail", " tiwtter", " facebook", " linkedin");

$Ubuzzwords = array("online-marketing", "social-media-marketing", "social-media-marketing", "social-media-marketing", 
                "social-media-marketing", "brand-marketing", "brand-marketing", "search-engine-optimisation", 
                "search-engine-optimisation", "online-marketing", "online-marketing", "online-marketing", 
                "online-marketing", "social-media-marketing", "social-media-marketing", "social-media-marketing");

$i = 0;

foreach ($Fbuzzwords as $buzzword)
{
    $url = $Ubuzzwords[$i];

    $Rbuzzwords = array(" <a href='".$base.$url."/'>online marketing</a>", " <a href='"
.$base.$url."/'>social media marketing</a>", 
                " <a href='".$base.$url."/'>social-media-marketing</a>", " <a href='".$base.$url."/'>social media</a>", 
                " <a href='".$base.$url."/'>social</a>", " <a href='".$base.$url."/'>brand marketing</a>", 
                " <a href='".$base.$url."/'>brand</a>", " <a href='".$base.$url."/'>search engine optimisation</a>", 
                " <a href='".$base.$url."/'>seo</a>", " <a href='".$base.$url."/'>email marketing</a>", 
                " <a href='".$base.$url."/'>email</a>", " <a href='".$base.$url."/'>e-mail marketing</a>", 
                " <a href='".$base.$url."/'>e-mail</a>", " <a href='".$base.$url."/'>tiwtter</a>", 
                " <a href='".$base.$url."/'>facebook</a>", " <a href='".$base.$url."/'>linkedin</a>");

    $replacement = $Rbuzzwords[$i];

    $text = preg_replace("/$buzzword/i", "$replacement", $text);

    $i++;
}

所以我有 3 个数组,1 个用于查找页面上的单词,2 个用于 url,我循环遍历每个部分的所有文本阵列确保我捕获每一个案例

I took a mixture of all of your answers and this is my finished result...

$Fbuzzwords = array(" online marketing", " social media marketing", " social-media-marketing", " social media", 
                " social", " brand marketing", " brand", " search engine optimisation", " seo", " email marketing", 
                " email", " e-mail marketing", " e-mail", " tiwtter", " facebook", " linkedin");

$Ubuzzwords = array("online-marketing", "social-media-marketing", "social-media-marketing", "social-media-marketing", 
                "social-media-marketing", "brand-marketing", "brand-marketing", "search-engine-optimisation", 
                "search-engine-optimisation", "online-marketing", "online-marketing", "online-marketing", 
                "online-marketing", "social-media-marketing", "social-media-marketing", "social-media-marketing");

$i = 0;

foreach ($Fbuzzwords as $buzzword)
{
    $url = $Ubuzzwords[$i];

    $Rbuzzwords = array(" <a href='".$base.$url."/'>online marketing</a>", " <a href='"
.$base.$url."/'>social media marketing</a>", 
                " <a href='".$base.$url."/'>social-media-marketing</a>", " <a href='".$base.$url."/'>social media</a>", 
                " <a href='".$base.$url."/'>social</a>", " <a href='".$base.$url."/'>brand marketing</a>", 
                " <a href='".$base.$url."/'>brand</a>", " <a href='".$base.$url."/'>search engine optimisation</a>", 
                " <a href='".$base.$url."/'>seo</a>", " <a href='".$base.$url."/'>email marketing</a>", 
                " <a href='".$base.$url."/'>email</a>", " <a href='".$base.$url."/'>e-mail marketing</a>", 
                " <a href='".$base.$url."/'>e-mail</a>", " <a href='".$base.$url."/'>tiwtter</a>", 
                " <a href='".$base.$url."/'>facebook</a>", " <a href='".$base.$url."/'>linkedin</a>");

    $replacement = $Rbuzzwords[$i];

    $text = preg_replace("/$buzzword/i", "$replacement", $text);

    $i++;
}

So I have 3 arrays, 1 for finding the word on the page, and 2 for the url, I loop though all of the text for each part of the array making sure I catch every case

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