像 facebook 一样实现 l.php 的好方法吗?
l.php是什么
我猜 l.php 是 link.php Facebook。它是一个预重定向 脚本。 Facebook 上的每个链接都是 过滤到 link.php?to=link
我有一些想法。就像
制作渲染类或使用正则表达式过滤输入一样,找到www.link.com,然后将其更改为类似
link
,问题是当我们进行一些更改时,如果我们更改某些内容,那么我们所有的档案链接都将不起作用(假设我们将输入放入数据库。 )。使用 JavaScript 来做到这一点,我的意思是是的,但我不熟悉 js,但我知道我们可以像上面那样实时搜索和更改链接。但问题是当用户没有打开 js 时(移动设备或没有 JavaScript 插件等)
或者您可能有更好的答案我们植入 l.php 的最佳方式是什么?
what is the l.php
im guessing l.php is link.php on
facebook. its a pre-redirecting
script. every link on facebook is
filtered to link.php?to=link
i have some things in mind. like
make a render class or filter the input with regex, finds an www.link.com then change it to something like
<a href="http://www.oursite.php?to=www.link.com">link</a>
, the problem is when we have some changes, if we change something then all our archives links isn't going to work ( assume that we put the input to the database. ).use a JavaScript to do this, i mean yes, but im not familiar with js, but i know we can search and change the links live like above. but the problem is when the user doesn't have a js on, ( mobile, or no JavaScript plugin, etc )
or maybe you have a better answer what is the best way we implant l.php ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用 PHP 让页面内容通过使用正则表达式替换链接的过滤器。
使用 javascript 意味着 paople 可以将其关闭。 PHP 确保所有链接都被正确替换。
请记住确保任何内部链接(如导航)保持不变。
有多种类型的正则表达式可以查找网址,您必须选择一种适合您需要的
编辑:我会使用此正则表达式
(https?://([-\w\.]+)+(:\d+ )?(/([\w/_\.]*(\?\S+)?)?)?)
I would have gone with using PHP to let the content of the page run through a filter that uses regex to replace the links.
Using javascript would mean that paople can turn it off. PHP ensures that all links get replaced correctly.
Just remember to make sure that any internal links (like navigation) is left untouched.
There are many types of regex that finds urls and you have to choose one that suits your needs
Edit: I would use this regex
(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)
这将是使用函数在视图中生成链接的优点之一:
您的
build_link()
会像这样:这意味着您甚至可以在链接中轻松使用 CDN。
最简单的选择是使用 Javascript,但就像你说的那样,依赖于用户拥有支持 Javascript 的浏览器,由于浏览器技术的进步,即使在移动平台上,这种情况也很可能发生。 jQuery 看起来像这样:
如果更改链接不可行,即您需要重写数据库或类似内容内的链接,我会考虑使用 DOMDocument ,这通常是避免使用的好习惯处理 HTML 等复杂结构时会出现混乱的正则表达式
因此,这里有三个选项:
检查网站上输出到外部网站的链接的每个部分,并通过
build_link
函数重写它们使用
DOMDocument
的帮助重写This would be one of the pros of using a function to generate links in your views:
Your
build_link()
would be like so:This means you could even use a CDN easily amongst your links.
The easiest option is to use Javascript, but like you say is reliant on the user to have Javascript enabled browser, which is highly likey due to the advance in browser technology, even in the mobile platform. The jQuery would look like so:
If changing your links is not viable, i.e. you need to rewrite links that is inside a database or similar, I would look into using
DOMDocument
-- it's generally good practice to avoid messy regexes when dealing with complex structures such as HTMLSo there are three options here:
Go through every part on your site that outputs links to external sites, and re-write them through a
build_link
functionRewrite using the help of
DOMDocument