扫描字符串并用链接替换标签
我有一个像这样的数组:
$keywords = array( 'php', 'html', 'css' );
我有一个数据库查询来返回一个段落,其中包含数组中前面提到的关键字。
我有一个像这样的链接模板:
$linktpl = '<a href="%s" title="%s">%s</a>';
我想要一个简单的函数来快速扫描该段落,每当它找到关键字时,它就会使用上面的链接模板将其转换为链接。
如果可能的话,我希望它考虑到单数和复数(例如框架和框架),
并且 SEO 进行此自动关键字链接是否安全?
有什么想法吗?
I have an array like this:
$keywords = array( 'php', 'html', 'css' );
I have a db query to return a paragraph, which contains the keywords previously mentioned in the array.
I have a link template like this:
$linktpl = '<a href="%s" title="%s">%s</a>';
I want a simple function to scan that paragraph and on the fly, whenever it finds a keyword it converts it to a link using the link template above.
And if possible I want it to take into account singular and plural (like framework and frameworks)
and is it safe for SEO to make this automated keyword linking?
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用正则表达式的优点是我们可以利用
\b
修饰符来确保捕获诸如(php)
、PHP.
或phpp
并正确处理它们。Advantage of using regular expressions is that we can leverage the
\b
modifier to ensure we catch cases such as(php)
,PHP.
, orphpp
and deal with them properly.这会起作用,但不一定是最好的方法。它将数组与管道字符连接起来,并使用该字符串构建正则表达式。
preg_replace()
然后完成剩下的工作。要求您更改链接模板以使用preg_replace()
样式而不是printf()
样式编辑:添加
\b
单词边界,因此您只匹配整个单词而不匹配内部子字符串。This will work but isn't necessarily the best way. It joins your array with pipe characters, and uses that string to build a regex.
preg_replace()
then does the rest. Requires that you change your link template to use thepreg_replace()
style instead of theprintf()
styleEDIT: added
\b
word boundaries so you only match whole words and not inner substrings.首先,这可能比看起来要复杂得多。也就是说,这将替换单词内的单词,IE 如果我们有
script
术语javascript
将是半链接,半单词。我不知道你是否在乎。解决此问题的一种方法是在单词前后添加空格。但话又说回来,这是个问题,标点符号又如何呢? (.,!?) 等。根据您的需要,您可能需要执行一些正则表达式并使之复杂化。还有一点需要注意的是,如果您的文本可以包含链接,您可以在链接中创建链接。
只是一些需要考虑的事项。我认为已经有很多这样的例子了,所以可能值得搜索这个网站看看你能找到什么。鉴于过于复杂,我无法提供该代码。如果您只需要简单的方法,其他人发布的方法应该可以正常工作。
一些参考:
用 php & 替换文本中的关键字mysql
First up, this can be way more complicated then it seems. Namely, this will replace words that are inside of a word, IE if we had
script
the termjavascript
would be half link, half word. I dunno if you care. One way to fix it, would be to add spaces before and after the word. But again, this as it's issues, as what about punctuations? (.,!?) etc.Depending on your needs you may need to do some regex and complicate it up. There is also the note that you could be creating links within links, if your text can contain links.
Just some items to think about. I think there are quite a few examples of this on SO already so it may be worth to search this site to see what you can find. Given the over complexity, I am not able to provide that code. If you just need the simple method, the others who have posted, should work just fine.
Some references:
Replacing keywords in text with php & mysql
对于您的主要问题,上述 3 个答案之一就足够了。
关于这个问题:
它足够安全..
但是有一些问题需要解决
检查此SEO 指南
谷歌。所以,总是更好
有好的锚文本。我假设
通过这种方法你不会得到
非常正确。
正如布拉德所解释的,不要做得太过分。
因此,可能只有 2-3 个
每页关键字,每页 1 个链接
段落中的关键字和总计
一个页面中有 6-7 个链接。你需要
小心不要有太多
链接。
“title属性指定了额外的
有关元素的信息。”所以
在那里只转储一个关键字
可能没有帮助。
对于 SEO 来说,采用手动方法总是比自动化方法更好。
For your main question, one of the above 3 answers should suffice.
Regarding this question :
It is safe enough..
But there are some concerns which need be addressed
Check page 13 in this SEO Guide by
Google. So, it is always better
to have good anchor text. I assume
through this method you won't get a
very proper one.
As Brad explained, don't overdo it.
Hence , may be have only 2-3
keywords per page, 1 link per
keyword in a paragraph and a total
of 6-7 links in a page. You need to
be careful in not having lot of
links.
"The title attribute specifies extra
information about an element." So
dumping just a keyword over there
may not help.
It is always better to go for manual methods rather than automation for SEO'ing your stuff.
str_replace()
几乎肯定是执行搜索/替换的最快方法,我建议您首先构建搜索词数组,然后构建替换,然后执行替换。
str_replace()
will almost definitely be the fastest way of performing the search/replaceI'd suggest you first build you array of search words, then the replacements and then perform the replace.
取决于你如何利用它。如果它太明显并且搜索引擎发现了一种模式,您可能有一天醒来会发现您的网站被 SERPS 禁止。
Depends on how you make use of it. If it's too obvious and search engines see a pattern you might wake up one day and find your sites banned from SERPS.