gerkintrigg Posted November 11, 2009 Share Posted November 11, 2009 Is there a way of using a database to check for links so they can automatically replace a word with a link to that word? Is there a way to do this with loads of words? I want to use a fairly substantial database to check web content against and alter the words output before the system outputs them. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/181165-str_replace-en-mass/ Share on other sites More sharing options...
roopurt18 Posted November 11, 2009 Share Posted November 11, 2009 All you really need is a map of words to urls. You can store that internally in PHP as an associative array or an array of objects. If you go that route then you'll have to manually edit PHP code to change associations. You could just as easily store this list in a database in a two-column table. As far as doing the find-and-replace algorithm, I don't think str_replace() will be suitable for your needs. Just examine the following code and its output: <?php $str = 'My dog is so nice.' . "\n"; echo $str; $str0 = str_replace( array( 'dog', 'nice' ), array( 'cat', 'mean' ), $str ); echo $str0; $str1 = str_replace( array( 'cat', 'dog' ), array( 'bird', 'cat' ), $str ); echo $str1; $str2 = str_replace( array( 'dog', 'cat' ), array( 'cat', 'bird' ), $str ); echo $str2; ?> My dog is so nice. My cat is so mean. My cat is so nice. My bird is so nice. As you can see, the order in which you declare the search and replacement terms is important. You'd probably want to write your own algorithm for parsing each word of the text you want to search over. strtok() may come in handy for this. Lastly, depending on the content-size of the document you're searching and the size of the replacement list, this could be an expensive operation in terms of computing resources required. As such, you may want to consider implementing some sort of caching feature. Link to comment https://forums.phpfreaks.com/topic/181165-str_replace-en-mass/#findComment-955819 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.