scan a string and replace tags with links

 ̄綄美尐妖づ 提交于 2021-02-04 19:45:29

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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.




回答5:


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);



回答6:


$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 );



回答7:


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.



来源:https://stackoverflow.com/questions/5889873/scan-a-string-and-replace-tags-with-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!