...

View Full Version : Add counter to text in simplexml element



kumba
07-30-2012, 07:15 PM
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:



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



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


[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

DrDOS
07-30-2012, 07:31 PM
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] .

mlseim
07-30-2012, 07:31 PM
oops... wrong answer, I didn't look close enough.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum