php追加xml子串

时间:2013-01-21 12:38:48

标签: php regex

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

 <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);

1 个答案:

答案 0 :(得分:1)

在大多数情况下*,您不应该使用正则表达式来解析HTML或XML。它不适合这项工作,而且很可能令人沮丧且容易出错。 See this question for better ways of doing this in PHP

话虽如此,你的正则表达式失败了,因为默认情况下,.与换行符不匹配。这意味着只有<Impression>...</Impression>才能匹配m

要解决此问题,您必须添加多行开关($pattern = '|<Impression>.*?</Impression>|m';):

{{1}}

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