Skip to content

Instantly share code, notes, and snippets.

@Achillefs
Created September 15, 2010 14:32
Show Gist options
  • Save Achillefs/580808 to your computer and use it in GitHub Desktop.
Save Achillefs/580808 to your computer and use it in GitHub Desktop.
<?php
class Phrase {
public static function to_link($content){
$phrases = DbFinder::from("Phrase")->find(); # This was for a symfony site
foreach ($phrases as $p) { # Interestingly, foreach is much faster than a for loop
$text = preg_quote($p->getText()); # Sanitize the text
$content = preg_replace("/(?!=(?:<a [^>]*>))({$text})(?!(?:<\/a>))/si","<a href=\"{$p->getUrl()}\">\\1</a>",$content,1);
}
return $content;
}
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment