将产品描述中的文字转换为链接
我有一个包含产品描述的电子商务网站,有时一个产品会描述该网站上的另一个产品,我想要一种链接到另一个产品的方法,而无需在描述中手动添加 标签。
我在想也许可以将描述与我的产品数据库进行比较,看看是否有任何单词与产品名称匹配,但我不确定如何去做,以及每次页面检查是否会对 mysql 数据库造成太大负载负载。
我想分解
描述中的空格,但有些产品中有空格,所以我认为这行不通。
I have an eccomerce site with product descriptions and sometimes a product will describe another product on the site and i want a way to link to the other product without manually putting the <a>
tag in the description.
I was thinking maybe have the description compared against my product database to see if any of the words match a product name, but im not sure how to go about that and if it would be to much load on the mysql database to check everytime the page loads.
I was thinking to explode
the whitespace in the description, but some products have spaces in them so i dont think that would work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以按如下方式处理此问题。
在数组中列出所有产品名称。您可以使用 APC、memcache 或类似的内容,如果您不想每次渲染页面时都查询数据库。
渲染站点时进行简单的字符串替换。还可以使用上面提到的机制来缓存结果,或者(如果您使用的是支持此功能的机制)借助模板系统的缓存功能。
但是,请注意由于产品名称过于宽泛而导致的误报。
简单的例子:
You could approach this as follows.
Have a list of all your product names in an array. You could cache this using APC, memcache or the like if you do not want to query the database for this everytime you render a page.
Do a simple string replacement when you're rendering the site. The result can also be cached either using the mechanisms named above or -- in case you're using one that supports this -- with help of the template system's caching functionality.
However, beware of false positives due to overly broad product names.
Simplistic example: