如何从网格内容中分离字符串并添加html元素


How to split string from meshed content and add html element

我从whois搜索结果中得到一个输出,作为内容的穿孔,我想在":"句子之前和":"之后添加粗体文本或任何其他HTML元素

一堆内容,例如

devims.com domain lookup results from whois.PublicDomainRegistry.com server: Domain Name: DEVIMS.COM Registry Domain ID: 1615408964_DOMAIN_COM-VRSN Registrar WHOIS Server: whois.publicdomainregistry.com Registrar URL: www.publicdomainregistry.com Updated Date: 2012-05-30T12:18:42Z Creation Date: 2010-09-12T14:54:29Z Registrar Registration Expiration Date: 2016-09-12T14:54:29Z Registrar: PDR Ltd. d/b/a PublicDomainRegistry.com Registrar IANA ID: 303 Registrar Abuse Contact Email: abuse-contact@publicdomainregistry.com Registrar Abuse Contact Phone: +1-2013775952 Domain Status: clientTransferProhibited (http://icann.org/epp#clientTransferProhibited) Registry Registrant ID: Registrant Name: devi sri mari Registrant Organization: devIMS Registrant Street: No.53, First Street, S.S Nagar, Thirumullaivoiyal, Registrant City: Chennai Registrant State/Province: Tamil Nadu Registrant Postal Code: 600062 Registrant Country: IN Registrant Phone: +044.42839080 Registrant Phone Ext: Registrant Fax: Registrant Fax Ext: Registrant Email: devi@devims.com Registry Admin ID: Admin Name: devi sri mari Admin Organization: devIMS Admin Street: No.53, First Street, S.S Nagar, Thirumullaivoiyal, Admin City: Chennai Admin State/Province: Tamil Nadu Admin Postal Code: 600062 Admin Country: IN Admin Phone: +044.42839080 Admin Phone Ext: Admin Fax: Admin Fax Ext: Admin Email: devi@devims.com Registry Tech ID: Tech Name: devi sri mari Tech Organization: devIMS Tech Street: No.53, First Street, S.S Nagar, Thirumullaivoiyal, Tech City: Chennai Tech State/Province: Tamil Nadu Tech Postal Code: 600062 Tech Country: IN Tech Phone: +044.42839080 Tech Phone Ext: Tech Fax: Tech Fax Ext: Tech Email: devi@devims.com Name Server: ns743.hostgator.com Name Server: ns744.hostgator.com DNSSEC:Unsigned URL of the ICANN WHOIS Data Problem Reporting System: http://wdprs.internic.net/ >>>Last update of WHOIS database: 2015-02-11T04:42:33+0000Z<<< For more information on Whois status codes, please visit https://icann.org/epp Registration Service Provided By: HOSTMACRO WEB SERVICES The data in this whois database is provided to you for information purposes only, that is, to assist you in obtaining information about or related to a domain name registration record. We make this information available "as is", and do not guarantee its accuracy. By submitting a whois query, you agree that you will use this data only for lawful purposes and that, under no circumstances will you use this data to: (1) enable high volume, automated, electronic processes that stress or load this whois database system providing you this information; or (2) allow, enable, or otherwise support the transmission of mass unsolicited, commercial advertising or solicitations via direct mail, electronic mail, or by telephone. The compilation, repackaging, dissemination or other use of this data is expressly prohibited without prior written consent from us. The Registrar of record is PDR Ltd. d/b/a PublicDomainRegistry.com. We reserve the right to modify these terms at any time. By submitting this query, you agree to abide by these terms

html中的预期输出

eg. <li>Registrar WHOIS Server</li>:
<li>whois.publicdomainregistry.com </li>
or 
eg. <b>Registrar WHOIS Server</b>: 
<li>whois.publicdomainregistry.com </li>

eg. <li>Registrar WHOIS Server</li>:
<li>whois.publicdomainregistry.com </li>
or 
eg. <b>Registrar WHOIS Server:</b> 
<li>whois.publicdomainregistry.com </li>

我尝试的内容:我的代码

  $result = "Registrar WHOIS Server: whois.publicdomainregistry.com Registrar URL: www.publicdomainregistry.com Updated Date: 2012-05-30T12:18:42Z Creation Date: 2010-09-12T14:54:29Z Registrar Registration Expiration Date: 2016-09-12T14:54:29Z Registrar: PDR Ltd. d/b/a PublicDomainRegistry.com Registrar IANA ID: 303 Registrar Abuse Contact Email: abuse-contact@publicdomainregistry.com Registrar Abuse Contact Phone: +1-2013775952 Domain Status: clientTransferProhibited (http://icann.org/epp#clientTransferProhibited) Registry Registrant ID: Registrant Name: devi sri mari Registrant Organization: devIMS Registrant Street: No.53, First Street, S.S Nagar, Thirumullaivoiyal, Registrant City: Chennai Registrant State/Province: Tamil Nadu Registrant Postal Code: 600062 Registrant Country: IN Registrant Phone: +044.42839080 Registrant Phone Ext: Registrant Fax: Registrant Fax Ext: Registrant Email: devi@devims.com";
        echo $result;
        $res = explode( ': ', $result );
        foreach ($res as $value) {
            echo '<li>'. $value .'</li>';
    }
        print_r( explode( ': ', $result ) );

输出

  Array
    (
        [0] => Registrar WHOIS Server
        [1] => whois.publicdomainregistry.com Registrar URL
        [2] => www.publicdomainregistry.com Updated Date
        [3] => 2012-05-30T12:18:42Z Creation Date
        [4] => 2010-09-12T14:54:29Z Registrar Registration Expiration Date
        [5] => 2016-09-12T14:54:29Z Registrar
        [6] => PDR Ltd. d/b/a PublicDomainRegistry.com Registrar IANA ID
        [7] => 303 Registrar Abuse Contact Email
        [8] => abuse-contact@publicdomainregistry.com Registrar Abuse Contact Phone
        [9] => +1-2013775952 Domain Status
        [10] => clientTransferProhibited (http://icann.org/epp#clientTransferProhibited) Registry Registrant ID
        [11] => Registrant Name
        [12] => devi sri mari Registrant Organization
        [13] => devIMS Registrant Street
        [14] => No.53, First Street, S.S Nagar, Thirumullaivoiyal, Registrant City
        [15] => Chennai Registrant State/Province
        [16] => Tamil Nadu Registrant Postal Code
        [17] => 600062 Registrant Country
        [18] => IN Registrant Phone
        [19] => +044.42839080 Registrant Phone Ext
        [20] => Registrant Fax
        [21] => Registrant Fax Ext
        [22] => Registrant Email
        [23] => devi@devims.com
    )

预期输出

 Array
    (
        [0] => Registrar WHOIS Server
        [1] => whois.publicdomainregistry.com 
        [2] => Registrar URL  
        [3] => www.publicdomainregistry.com
        [4] => Updated Date
        [5] => 2012-05-30T12:18:42Z
        [6] => reation Date
        [7] => 2010-09-12T14:54:29Z
        [8] => Registrar Registration Expiration Date
        [9] => 2016-09-12T14:54:29Z
        [10] => Registrar URL
        [11] => PDR Ltd. d/b/a PublicDomainRegistry.com
        [12] => Registrar IANA ID
        ....
    )

我想说,这里真正的问题是您已经从whois输出中删除了换行符。如果您保留了换行符,您应该能够更好地将键与值分离。现在没有很好的方法来判断什么是值的一部分,什么是下一个键的一部分。这就是为什么结果输出会将它们混淆的原因。

所以,试着在不去掉换行符的情况下获得whois输出。然后你可以这样做:

// Note: Newlines added to $result below (and string shortened a bit)
$result = "Registrar WHOIS Server: whois.publicdomainregistry.com'nRegistrar URL: www.publicdomainregistry.com'nUpdated Date: 2012-05-30T12:18:42Z'nCreation Date: 2010-09-12T14:54:29Z'nRegistrar Registration Expiration Date: 2016-09-12T14:54:29Z'nRegistrar: PDR Ltd. d/b/a PublicDomainRegistry.com";
$out = array();
$rows = explode("'n", $result);
foreach ($rows as $row) {
    preg_match('/(.*?)': (.*)/', $row, $matches);
    $out[] = $matches[1]; // Key
    $out[] = $matches[2]; // Value
}
print_r($out);

输出:

Array
(
    [0] => Registrar WHOIS Server
    [1] => whois.publicdomainregistry.com
    [2] => Registrar URL
    [3] => www.publicdomainregistry.com
    [4] => Updated Date
    [5] => 2012-05-30T12:18:42Z
    [6] => Creation Date
    [7] => 2010-09-12T14:54:29Z
    [8] => Registrar Registration Expiration Date
    [9] => 2016-09-12T14:54:29Z
    [10] => Registrar
    [11] => PDR Ltd. d/b/a PublicDomainRegistry.com
)

如果你真的坚持使用没有换行符的whois输出,你必须知道你感兴趣的所有键,并在此基础上进行键和值之间的分隔,而不仅仅是冒号:

$result = "Registrar WHOIS Server: whois.publicdomainregistry.com Registrar URL: www.publicdomainregistry.com Updated Date: 2012-05-30T12:18:42Z Creation Date: 2010-09-12T14:54:29Z Registrar Registration Expiration Date: 2016-09-12T14:54:29Z Registrar: PDR Ltd. d/b/a PublicDomainRegistry.com Registrar IANA ID: 303 Registrar Abuse Contact Email: abuse-contact@publicdomainregistry.com Registrar Abuse Contact Phone: +1-2013775952 Domain Status: clientTransferProhibited (http://icann.org/epp#clientTransferProhibited) Registry Registrant ID: Registrant Name: devi sri mari Registrant Organization: devIMS Registrant Street: No.53, First Street, S.S Nagar, Thirumullaivoiyal, Registrant City: Chennai Registrant State/Province: Tamil Nadu Registrant Postal Code: 600062 Registrant Country: IN Registrant Phone: +044.42839080 Registrant Phone Ext: Registrant Fax: Registrant Fax Ext: Registrant Email: devi@devims.com";
$keys = array("Registrar WHOIS Server", "Registrar URL", "Registrar WHOIS Server", "Registrar URL", "Updated Date", "Creation Date", "Registrar Registration Expiration Date", "Registrar", "Registrar IANA ID", "Registrar Abuse Contact Phone", "Domain Status", "Registry Registrant ID", "Registrant Name", "Registrant Organization", "Registrant City", "Registrant State'/Province", "Registrant Postal Code", "Registrant Country", "Registrant Phone", "Registrant Fax", "Registrant Fax Ext", "Registrant Email");
$keysImploded = implode('|', $keys);
$out = array();
while (preg_match('/(' . $keysImploded . '): (.*)/', $result, $matches)) {
    $out[]  = $matches[1];
    $result = $matches[2];
    preg_match('/(.*?)((' . $keysImploded . ': |$).*)/', $result, $matches);
    $out[]  = $matches[1];
    $result = $matches[2];
}
print_r($out);

输出:

Array
(
    [0] => Registrar WHOIS Server
    [1] => whois.publicdomainregistry.com
    [2] => Registrar URL
    [3] => www.publicdomainregistry.com
    [4] => Updated Date
    [5] => 2012-05-30T12:18:42Z
    [6] => Creation Date
    [7] => 2010-09-12T14:54:29Z
    [8] => Registrar Registration Expiration Date
    [9] => 2016-09-12T14:54:29Z
    [10] => Registrar
    [11] => PDR Ltd. d/b/a PublicDomainRegistry.com
    ....
)

然而,这种方法要求您事先知道whois结果字符串中有哪些(可能的)键,并且那些后跟冒号和空格的键名(例如"Registrar:")不会出现在值字符串中的任何位置。