Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add counter to text in simplexml element

    I'm trying to add a counter for each result of a regular expression in an xml element. But not having much success.

    This is my xml structure:

    Code:
    <content>
      <article>
        <headline>HEADLINE1</headline>
            <date>DATE1</date>
                <copy_intro>COPY INTRO1</copy_intro>
                    <copy_full>
                    JACKANDJILL - Went up the hill
                    Out Cue...the hill    Length: 03 Seconds
    
                    TOFETCH - a pail of water 
                    Out Cue...of water    Length: 04 Seconds                    
                    </copy_full>
      </article>
      <article>
        <headline>HEADLINE2</headline>
            <date>DATE2</date>
                <copy_intro>COPY INTRO2</copy_intro>
                    <copy_full>
                    JACKFELLDOWN - and broke his crown 
                    Out Cue...his crown   Length: 04 Seconds
    
                    ANDJILLCAME - tumbling after
                    Out Cue...tumbling   Length: 04 Seconds
                    </copy_full>
      </article>
    </content>
    This is the result I've been trying to achieve:

    Code:
    HEADLINE1
    DATE1
    COPY INTRO1
    [Clip #: 01] JACKANDJILL - Went up the hill
    Out Cue...the hill    Length: 03 Seconds
    
    [Clip #: 02] TOFETCH - a pail of water 
    Out Cue...of water    Length: 04 Seconds
    
    HEADLINE2
    DATE2
    COPY INTRO2
    [Clip #: 03] JACKFELLDOWN - and broke his crown 
    Out Cue...his crown   Length: 04 Seconds
    
    [Clip #: 04] ANDJILLCAME - tumbling after
    Out Cue...tumbling   Length: 04 Seconds
    Here is the PHP that I'm having trouble with:

    PHP Code:
    <?php
    $xml 
    simplexml_load_file('../daily_log/dailylog.xml');

    foreach(
    $article as $article){      
        
    $xmlheadline $article->headline;
        
    $xmldate $article->date;
        
    $xmlcopyintro $article->copy_intro;
        
    $xmlcopy $article->copy_full;

        
    //This is the loop I'm having a problem with:
        
    foreach($article->copy_full as $xmlcopy){
            
    $i=0;
            
    $regexp "/[A-Z]{4,}|\b[A-Z]\b/"//Matching 4 concurrent ALLCAP letters 
            
    if (preg_match($regexp$xmlcopy$addclip)) {          
                
    $addclip "[Clip #: ".$i++."]".$addclip;
                echo 
    $addclip;          
        }
    }
    $xmlcopy nl2br($xmlcopy);
    echo 
    "<p>";
    echo 
    "<strong>".$xmlheadline."</strong><br/>";
    echo 
    "<strong>".$xmlcopyintro."</strong><br/>";
    echo 
    "<strong>".$xmldate."</strong><br/>";
    echo 
    $xmlcopy."<br/>";
    echo 
    "</p>";
    }
    ?>
    The output I keep getting for $addclip:
    Code:
    [Clip #: 0]Array[Clip #: 0]Array
    Thanks for taking a moment to look at this and please excuse the amateur coding as I am fairly new to this.
    Best, Kumba

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,903
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Addclip is an array, so it's doing exactly what you told it to. You want to echo $i, which is the number of instances the occurrence.
    And for each occurrencve it's $addclip[$i] .
    Last edited by DrDOS; 07-30-2012 at 06:35 PM.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    oops... wrong answer, I didn't look close enough.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •