【问题标题】:How can I break an url and store the key-words into database using php如何使用 php 打破 url 并将关键字存储到数据库中
【发布时间】:2009-12-10 09:06:42
【问题描述】:

点赞http:webmail.wipro.com#a:?b;

我想破解这个 url,只将 webmail 和 wipro 存储到我的数据库中。任何人都可以帮我解决这个问题。使用 php。

【问题讨论】:

  • 首先我想问你澄清你的问题是什么:我不知道你是否想要关键词(对我来说这意味着你想要变量,也许是'a'和'b '?) 或存储在数据库中的子域 ('webmail') 和域 ('wipro')。另外,您使用的是什么数据库?
  • 我需要存储在我的数据库 mysql 中的子域 ('webmail') 和域 ('wipro')

标签: php regex parsing url-parsing


【解决方案1】:

您应该使用parse_url 函数来检索零件,然后随意使用(在您的情况下,将它们保存在数据库中)。

这是手册中的测试代码/输出:

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);

打印以下内容:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)
/path

【讨论】:

    【解决方案2】:

    您应该使用正则表达式。如果你运行类似

    preg_match('http:(.*?).(.*?).com#a:?b;', 'http:webmail.wipro.com#a:?b;', $matches);
    

    $matches[1] 应该是 webmail,$matches[2] 应该包含 wipro。

    有更多关于正则表达式和 preg_match on the PHP site 的文档。

    【讨论】:

    【解决方案3】:

    听起来您正在寻找的是识别 URL 中的任何单词。在这种情况下,试试这个 RegExp:

    preg_match_all ('/\b(\w{4,})\b/', $url, $matches);
    

    $matches 将包含一个由所有长度为 4 或更长的类似单词的字符串组成的数组

    【讨论】:

    • webmail.wipro.com"; preg_match_all ('/\b(\w{4,})\b/', $url, $matches); $n=count($matches);回声 $n; for($i = 0; $i "; } ?> 输出:2Piece 0 = Array Piece 1 = Array
    • 为什么我得到这样的输出请帮帮我
    • $matches 不包含这样的长度为 4 的单词
    • preg_match_all 也返回内部匹配的子数组(即..(\w{4,0})..)。你要的字在$matches[1]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2016-03-17
    相关资源
    最近更新 更多