扫描字符串并用链接替换标签

发布于 2024-11-05 09:59:29 字数 377 浏览 6 评论 0原文

我有一个像这样的数组:

$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 技术交流群。

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

发布评论

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

评论(7

怼怹恏 2024-11-12 09:59:29
$string = 'this is the php test subject.';

// associate keywords with their urls
$urls = array(
'php' => 'http://www.php.net',
// and etc...
);

// this callback will take the matches from preg and generate the
// html link making use of the $urls dictionary
$linker = function($matches) use($urls) {
    $urlKey = strtolower($matches[1]);
    return sprintf(
      '<a href="%s" title="%s">%s</a>',
      $urls[$urlKey], $matches[1], $matches[1]
    );
};

// do the magic
$regex = '/\b(' . preg_quote(implode('|', $keywords), '/') . ')\b/i';
preg_replace_callback($regex, $linker, $string);

使用正则表达式的优点是我们可以利用 \b 修饰符来确保捕获诸如 (php)PHP.phpp并正确处理它们。

$string = 'this is the php test subject.';

// associate keywords with their urls
$urls = array(
'php' => 'http://www.php.net',
// and etc...
);

// this callback will take the matches from preg and generate the
// html link making use of the $urls dictionary
$linker = function($matches) use($urls) {
    $urlKey = strtolower($matches[1]);
    return sprintf(
      '<a href="%s" title="%s">%s</a>',
      $urls[$urlKey], $matches[1], $matches[1]
    );
};

// do the magic
$regex = '/\b(' . preg_quote(implode('|', $keywords), '/') . ')\b/i';
preg_replace_callback($regex, $linker, $string);

Advantage of using regular expressions is that we can leverage the \b modifier to ensure we catch cases such as (php), PHP., or phpp and deal with them properly.

东京女 2024-11-12 09:59:29

这会起作用,但不一定是最好的方法。它将数组与管道字符连接起来,并使用该字符串构建正则表达式。 preg_replace() 然后完成剩下的工作。要求您更改链接模板以使用 preg_replace() 样式而不是 printf() 样式

preg_replace("/\b(" . implode("|", $keywords) .")\b/", "<a href='\\1'>\\1</a>", $paragraph);

编辑:添加 \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 the preg_replace() style instead of the printf() style

preg_replace("/\b(" . implode("|", $keywords) .")\b/", "<a href='\\1'>\\1</a>", $paragraph);

EDIT: added \b word boundaries so you only match whole words and not inner substrings.

转角预定愛 2024-11-12 09:59:29
$paragraph = /* YOUR PARAGRAPH CONTENT */;
$paragraph = str_replace( array( 'php' , 'html' , 'css' ) , array( '<a href="url/php/" title="php">PHP</a>' , '<a href="url/html/" title="html">HTML</a>' , '<a href="url/css/" title="css">CSS</a>' ) , $paragraph );
$paragraph = /* YOUR PARAGRAPH CONTENT */;
$paragraph = str_replace( array( 'php' , 'html' , 'css' ) , array( '<a href="url/php/" title="php">PHP</a>' , '<a href="url/html/" title="html">HTML</a>' , '<a href="url/css/" title="css">CSS</a>' ) , $paragraph );
π浅易 2024-11-12 09:59:29

首先,这可能比看起来要复杂得多。也就是说,这将替换单词内的单词,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 term javascript 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

只有影子陪我不离不弃 2024-11-12 09:59:29

对于您的主要问题,上述 3 个答案之一就足够了。

关于这个问题:

and is it safe for SEO to make this automated keyword linking?

它足够安全..

但是有一些问题需要解决

  1. 检查此SEO 指南
    谷歌。所以,总是更好
    有好的锚文本。我假设
    通过这种方法你不会得到
    非常正确。

  2. 正如布拉德所解释的,不要做得太过分。
    因此,可能只有 2-3 个
    每页关键字,每页 1 个链接
    段落中的关键字和总计
    一个页面中有 6-7 个链接。你需要
    小心不要有太多
    链接。

  3. “title属性指定了额外的
    有关元素的信息。”所以
    在那里只转储一个关键字
    可能没有帮助。

对于 SEO 来说,采用手动方法总是比自动化方法更好。

For your main question, one of the above 3 answers should suffice.

Regarding this question :

and is it safe for SEO to make this automated keyword linking?

It is safe enough..

But there are some concerns which need be addressed

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

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

  3. "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.

辞慾 2024-11-12 09:59:29

str_replace() 几乎肯定是执行搜索/替换的最快方法,

我建议您首先构建搜索词数组,然后构建替换,然后执行替换。

$searches = array("php", "html", "css");
$replacements = array();
while($row = mysql_fetch_assoc($r) {
    $replacements[] = sprintf($linktmpl, $row['url'], $row['title'], $row['word']);
}
$html = str_replace($searches, $replacements, $html);

str_replace() will almost definitely be the fastest way of performing the search/replace

I'd suggest you first build you array of search words, then the replacements and then perform the replace.

$searches = array("php", "html", "css");
$replacements = array();
while($row = mysql_fetch_assoc($r) {
    $replacements[] = sprintf($linktmpl, $row['url'], $row['title'], $row['word']);
}
$html = str_replace($searches, $replacements, $html);
兔姬 2024-11-12 09:59:29

SEO 进行此自动关键字链接是否安全?

取决于你如何利用它。如果它太明显并且搜索引擎发现了一种模式,您可能有一天醒来会发现您的网站被 SERPS 禁止。

and is it safe for SEO to make this automated keyword linking?

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.

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