doudou5023 2010-07-16 09:02
浏览 29
已采纳

如何使用PHP从段落中提取特定单词?

hi for example this is a plain text paragraph:-

Domain Name: COMCAST.NET
Registrar: CSC CORPORATE DOMAINS, INC.
Whois Server: whois.corporatedomains.com
Referral URL: http://www.cscglobal.com
Name Server: DNS101.COMCAST.NET
Name Server: DNS102.COMCAST.NET
Name Server: DNS103.COMCAST.NET
Name Server: DNS104.COMCAST.NET
Name Server: DNS105.COMCAST.NET
Status: clientTransferProhibited
Updated Date: 21-jan-2010
Creation Date: 25-sep-1997
Expiration Date: 24-sep-2012

How do I extract particular words using PHP??

say I need Registrar,Name Servers and status. I need it in different variables. Name server variables can be in array as it is more than one.

  • 写回答

2条回答 默认 最新

  • douyou3619 2010-07-16 09:11
    关注

    Here's a snippet that should do as requested:

    $lines = explode("
    ", $data);
    
    $output = array();
    foreach($lines as $line)
    {
        list($key, $value) = explode(': ', $line, 2);
        if(isset($output[$key]))
        {
            if(!is_array($output[$key]))
            {
                $tmp_val = $output[$key];
                $output[$key] = array($tmp_val);
            }
            $output[$key][] = $value;
        }
        else
        {
            $output[$key] = $value;
        }
    }
    print_r($output);
    

    What it does is:

    • It splits the data in lines
    • Gets the key/value pairs
    • Than it appends it to the output array, creating an extra nesting level on duplicate keys

    The output is:

    Array
    (
        [Domain Name] => COMCAST.NET
        [Registrar] => CSC CORPORATE DOMAINS, INC.
        [Whois Server] => whois.corporatedomains.com
        [Referral URL] => http://www.cscglobal.com
        [Name Server] => Array
            (
                [0] => DNS101.COMCAST.NET
                [1] => DNS102.COMCAST.NET
                [2] => DNS103.COMCAST.NET
                [3] => DNS104.COMCAST.NET
                [4] => DNS105.COMCAST.NET
            )
    
        [Status] => clientTransferProhibited
        [Updated Date] => 21-jan-2010
        [Creation Date] => 25-sep-1997
        [Expiration Date] => 24-sep-2012
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄