如何从网格内容中拆分字符串并添加html元素 [英] How to split string from meshed content and add html element

查看:68
本文介绍了如何从网格内容中拆分字符串并添加html元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从whois搜索结果中得到一个输出,作为内容的重点,我想将粗体文本或在':'句子之前以及':'

I got a ouput from whois search result as punch of content i want to make bold text or add any other HTML element before the ': ' Sentence and also after ': '

一堆内容,例如

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输出中删除了换行符.如果保留换行符,则应该能够更好地将键与值分开.现在,没有什么好办法来告诉值是什么,下一个键是什么,这就是为什么输出结果将它们混合在一起的原因.

I would say that the real problem here is that you have removed the newlines from the whois output. Had you kept the newlines, you should have been able to better separate the keys from the values. Now there is no good way of telling what is part of a value and what is part of the next key, which is why the resulting output have them mixed up.

因此,请尝试在不删除换行符的情况下获取whois输出.然后,您可以执行以下操作:

So, try to get hold of the whois output without the newlines stripped off. Then you can do this:

// 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输出感到困惑,那么您将必须了解您感兴趣的所有键,并以此为基础在键和值之间进行分隔,而不仅仅是冒号:

If you really are stuck with the whois output without newlines, you will have to know all keys you are interested in and make the separation between keys and values based on that, rather than just the colons:

$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:")在列表中的任何位置都不会出现.值字符串.

This approach, however, requires that you know beforehand what (possible) keys there are in the whois result string and that those key names followed by a colon and a space (e.g. "Registrar: ") does not occur anywhere in the value strings.

这篇关于如何从网格内容中拆分字符串并添加html元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆