0

In the below xml i have to append all the url which comes under impression tag.

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

In existing content between <Impression> and </Impression>, would like to add more string.

how can i achieve this?

i am trying something like this

$pattern = '<Impression>.*?</Impression>';      
preg_match($pattern, $jsaved, $matchesarr);
Naveed S
  • 5,106
  • 4
  • 34
  • 52
Jeet Singh
  • 402
  • 1
  • 5
  • 12
  • 1
    Don't use a regex for this task. See this question for better ways of parsing XML/HTML: http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-xml-with-php/3577662#3577662 –  Jan 21 '13 at 12:52

1 Answers1

1

In most cases*, you should not use a regex for parsing HTML or XML. It is not the right tool for the job, and it is likely to be frustrating and error-prone. See this question for better ways of doing this in PHP.

That being said, your regex is failing because, by default, . does not match a newline character. This means that you will only match <Impression>...</Impression> if it all occurs on a single line.

To fix this, you have to add the multi-line switch (m):

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

**There are exceptions. A regex can be fine if your input is very well-defined and always follows a predictable format. But it is definitely worth considering whether an XML parser would be better.*

Community
  • 1
  • 1
  • Additionally, you shouldn't use `.*?` but rather `.*` - ? means "the previous, zero or one time" and * means "the previous, any number of times (including zero)", so the ? is redundant. – Magnus Jan 21 '13 at 13:00
  • @Magnus, that's not correct. The `?` is necessary so that each opening tag matches the first subsequent closing tag. If you remove it, you will get one huge match from the first opening tag to the final closing tag. –  Jan 21 '13 at 13:03
  • ah right, I keep forgetting that Perl and PHP regexen aren't actually *regular*. *sigh*, sorry my bad. – Magnus Jan 21 '13 at 13:04
  • Just to clarify: `?` following `*` has a special meaning: it sets the "greediness" of the match. By default, `*` is greedy and matches the largest amount possible. Adding `?` makes it non-greedy, matching the smallest amount possible. –  Jan 21 '13 at 13:04
  • Aye, that's what I meant, I forgot that these aren't actual *regular expressions* but rather what the Perl crowd made of them. Although I do see that coming in handy, heh. – Magnus Jan 21 '13 at 13:09