如何查找并识别字符串的特定部分?
我正在创建一个搜索网站,用户可以在其中找到我的桌子上的东西。
搜索效果很好,但我希望能够允许用户使用 Google 或 Bing 结果进行搜索。我希望人们通过在查询后指示 / 来确定他们想要使用特定的搜索引擎。
例如,搜索something /google会将搜索发送到Google。对于必应来说也是如此。
I am making a search site where users can find stuff with my table.
Searching works great, but I want the ability to allow users to search instead with Google or Bing result. I would like people to identify that they want to use a specific search engine by indicating a / after the query.
So for example a search for something /google would send the search to Google. Likewise for Bing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在寻找 Google 和 Bing 或少量搜索引擎,您可以尝试:
注意,您需要在将任何文本输出到 PHP 的输出缓冲区之前将其添加到您的页面(由于 header() 函数调用)。这意味着,在 PHP 被指示将任何内容写入页面之前。否则,您的 PHP 页面将会出现错误。
If you're looking for Google and Bing, or a small number of search engines, you can try:
Note, you need to add this to your page before any text is outputted to PHP's output buffer (due to the header() function call). This means, before PHP has been instructed to write anything out to the page. Otherwise, you will get an error on your PHP page.
您可以使用正则表达式来提取查询字符串的这些部分。例如,
将提取以斜线开头且后跟查询中的一个或多个字母的所有标记。然后,您可以通读这些标记并决定对每个标记采取什么操作。
此正则表达式解决方案非常强大,因为它应该允许这些(以空格分隔的)标记出现在查询字符串中的任何位置,甚至允许在查询字符串中包含多个标记。
然后,您可能还想使用
preg_replace(...)
从查询中删除这些标记,以便您可以处理搜索文本的其余部分。You could use a regexp to extract these parts of the query string. e.g.
Would pull out all tokens that started with a slash and were followed by one or more letters from your query. You could then read through these tokens and decide what action to take on each one.
This regexp solution is robust because it should allow these (space separated) tokens to appear anywhere in your query string, or even to have multiple tokens in a query string.
You might then also want to use
preg_replace(...)
to strip these tokens from the query so that you can process the rest of the search text.使用 preg_match 检查
/search_engine
搜索字符串的最后部分。如果存在,则将其重定向到
http://www.google。 com/search?q=site%3Adomain.com+search+string
use preg_match to check the last part of the search string for
/search_engine
.if it exists then redirect them to
http://www.google.com/search?q=site%3Adomain.com+search+string