0

在下面的 xml 中,我必须附加印象标记下的所有 url。

 <Ad id="32716">        
<InLine>            
    <AdSystem>BrightRoll</AdSystem>         
    <AdTitle/>          
    <Impression><![CDATA[http://2901.btrll.com/imp/2901/12446/PreRoll.911.123806/start;Video;1358769313]]>
    </Impression>           
    <Impression><![CDATA[http://brxserv.btrll.com/v1/epix/6835361/3850628/20799/32716/TbwKgCbwBkAABQ_SyhAABRPwAAf8wAOsGEAAAAAAAfYQiqTYrVDQ/event.imp/r_64.aHR0cDovL2Iuc2NvcmVjYXJkcmVzZWFyY2guY29tL3A_YzE9MSZjMj02MDAwMDA2JmMzPSZjND1icngmYzU9MDEwMDAwJmM2PTY4MzUzNjEmYzEwPSZjQTE9OCZjQTI9NjAwMDAwNiZjQTM9MjA3OTkmY0E0PTM4NTA2MjgmY0E1PTQ3NDcmY0E2PTY4MzUzNjEmY0ExMD0zMjcxNiZjdj0xLjcmY2o9JnJuPTEzNTg3NjkzMTMmcj1odHRwJTNBJTJGJTJGcGl4ZWwucXVhbnRzZXJ2ZS5jb20lMkZwaXhlbCUyRnAtY2I2QzB6RkY3ZFdqSS5naWYlM0ZsYWJlbHMlM0RwLjY4MzUzNjEuMzg1MDYyOC4wJTJDYS40NzQ3LjIwNzk5LjMyNzE2JTJDdS45NjguMHgwJTNCbWVkaWElM0RhZCUzQnIlM0QxMzU4NzY5MzEz]]>
    </Impression>           
    <Impression><![CDATA[http://ib.adnxs.com/seg?add=362896&t=2&cb=1358769313]]>
    </Impression>           
    <Impression><![CDATA[http://rc.rlcdn.com/361686.gif]]>
    </Impression>           
    <Impression><![CDATA[http://mobile.btrll.com/event/impression?ts=1358769313920&siteId=3850628&lineItemId=20799]]>
    </Impression>           
    <Creatives>             
        <Creative sequence="1">                 
            <Linear>                        
                <Duration>00:00:14</Duration>                       
                <TrackingEvents>                            
                    <Tracking event="midpoint"><![CDATA[http://brxserv.btrll.com/v1/epix/6835361/3850628/20799/32716/TbwKgCbwBkAABQ_SyhAABRPwAAf8wAOsGEAAAAAAAfYQiqTYrVDQ/event.mid/r_64.aHR0cDovLzI5MDEuYnRybGwuY29tL2ltcC8yOTAxLzEyNDQ2L1ByZVJvbGwuOTExLjEyMzgwNi9taWQ7VmlkZW87MTM1ODc2OTMxMw]]>
                    </Tracking>                         
                    <Tracking event="complete"><![CDATA[http://brxserv.btrll.com/v1/epix/6835361/3850628/20799/32716/TbwKgCbwBkAABQ_SyhAABRPwAAf8wAOsGEAAAAAAAfYQiqTYrVDQ/event.end/r_64.aHR0cDovLzI5MDEuYnRybGwuY29tL2ltcC8yOTAxLzEyNDQ2L1ByZVJvbGwuOTExLjEyMzgwNi9kb25lO1ZpZGVvOzEzNTg3NjkzMTM]]>
                    </Tracking>                         
                    <Tracking event="complete"><![CDATA[http://mobile.btrll.com/event/completion?ts=1358769313921&siteId=3850628&lineItemId=20799]]>
                    </Tracking>                     
                </TrackingEvents>                       
                <VideoClicks>                           
                    <ClickThrough><![CDATA[http://brxserv.btrll.com/v1/epix/6835361/3850628/20799/32716/TbwKgCbwBkAABQ_SyhAABRPwAAf8wAOsGEAAAAAAAfYQiqTYrVDQ/event.click/r_64.aHR0cDovLzI5MDEuYnRybGwuY29tL2Nsay8yOTAxLzEyNDQ2L1ByZVJvbGwuOTExLjEyMzgwNi9ub25lLztWaWRlbzsxMzU4NzY5MzEz]]>
                    </ClickThrough>                         
                    <ClickTracking><![CDATA[http://brxserv.btrll.com/v1/epix/6835361/3850628/20799/32716/TbwKgCbwBkAABQ_SyhAABRPwAAf8wAOsGEAAAAAAAfYQiqTYrVDQ/event.c_trk/r_64.aHR0cDovLzI5MDEuYnRybGwuY29tL2Nsay8yOTAxLzEyNDQ2L1ByZVJvbGwuOTExLjEyMzkyNy9ub25lLztWaWRlbzsxMzU4NzY5MzEz]]>
                    </ClickTracking>                            
                    <ClickTracking><![CDATA[http://mobile.btrll.com/event/click?ts=1358769313921&siteId=3850628&lineItemId=20799]]>
                    </ClickTracking>                        
                </VideoClicks>                      
                <MediaFiles>                            
                    <MediaFile delivery="progressive" type="video/mp4" bitrate="400" height="240" width="320"><![CDATA[http://brxcdn2.btrll.com/production/134980/Home_Depot_MuddyBoots_640x480_15sec.mp4]]>
                    </MediaFile>                        
                </MediaFiles>                   
            </Linear>                   
        </Creative>             
        <Creative sequence="1">                 
            <CompanionAds>                  </CompanionAds>             
        </Creative>         
    </Creatives>        
</InLine>   

在现有内容 和 之间<Impression></Impression>想添加更多字符串。

我怎样才能做到这一点?

我正在尝试这样的事情

$pattern = '<Impression>.*?</Impression>';      
preg_match($pattern, $jsaved, $matchesarr);
4

1 回答 1

1

在大多数情况下*,您不应使用正则表达式来解析 HTML 或 XML。它不是适合这项工作的工具,而且很可能令人沮丧且容易出错。 有关在 PHP 中执行此操作的更好方法,请参阅此问题

话虽如此,您的正则表达式失败,因为默认情况下,.它不匹配换行符。<Impression>...</Impression>这意味着只有当它全部出现在一行时,您才会匹配。

要解决此问题,您必须添加多行开关 ( m):

$pattern = '|<Impression>.*?</Impression>|m';

**有例外。如果您的输入定义非常明确并且始终遵循可预测的格式,那么正则表达式就可以了。但绝对值得考虑 XML 解析器是否会更好。*

于 2013-01-21T12:57:38.457 回答