preg_replace效率
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者,如果维持大写并不重要:
Or, if maintaining capitalisation is not important:
函数 preg_replace 可以接收第二个替换数组。
如果您打算始终使用相同的语法,则可以执行循环来生成链接(而不是每次想要拥有元素时自己编写)
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)
您可以将 str_ireplace() 与数组一起使用(这是不区分大小写的匹配字符串替换),因此如果找到某个匹配项,它将用另一个数组中的相应替换项替换它。
例如:
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:
我混合了你所有的答案,这是我的最终结果...
所以我有 3 个数组,1 个用于查找页面上的单词,2 个用于 url,我循环遍历每个部分的所有文本阵列确保我捕获每一个案例
I took a mixture of all of your answers and this is my finished result...
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