Forum Moderators: coopster

Message Too Old, No Replies

php output wordwrap xml node

         

j milo taylor

10:30 am on Aug 14, 2009 (gmt 0)

10+ Year Member



Hi,

I guess this must be pretty simple but my brain is refusing to deliver. I want to put a 8 word length limit on one node of an php query that outputs an xml file.

psuedo code:
$xml_output .= "<artist_works>\n";

for($x =0 ; $x < mysql_num_rows($rs_artist_details) ; $x++){
$row_rs_artist_details = mysql_fetch_assoc($rs_artist_details);

$xml_output .= "<works_details>\n";
$xml_output .= "<Artist_ID>" . $row_rs_artist_details['id_artist'] . "</Artist_ID>\n";
$xml_output .= "<Artist_DOB>" . $row_rs_artist_details['year_artist'] . "</Artist_DOB>\n";
(snip)
--- just a wordwrap here
$xml_output .= "<Work_Notes>" . $row_rs_artist_details['notes_work'] . "</Work_Notes>\n";
--to here
$xml_output .= "<Work_Image>" . $row_rs_artist_details['image_image'] . "</Work_Image>\n";

--- etc.

$xml_output .= "</works_details>\n ";
}

$xml_output .= "</artist_works>\n";

$filename = 'XML-ARTIST_SELECTED_WORKS-' . $row_rs_artist_details['1stname_artist'] . $row_rs_artist_details['2ndname_artist'] . '.xml';
if (($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open ($filename)";
exit;
}

can anyone help?

milo

andrewsmd

6:13 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you mean 8 word limit?

j milo taylor

6:44 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



sorry for not being clear,

to force a <br /> after 8 words

andrewsmd

8:08 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming your words are separated by a blank white space use this.

$xml_output .= "<Work_Notes>" . returnWord($row_rs_artist_details['notes_work']) . "</Work_Notes>\n";

function returnWord($string){

$count = 1;
$tempArr = split(" ", $string);
foreach($tempArr as $i){

if($count % 8 == 0){
$returnStr .= $i."<br>"." ";
$count = 1;
}//if
else{
$returnStr .= $i." ";
$count++;
}//else

}//foreach
return ($returnStr);
}//returnWord

bkeep

8:32 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



You could also try the wordwrap function

[us3.php.net...]

andrewsmd

8:38 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes but word_wrap will cut up words, that function will return on a line space. So if somebody entered a word like I reaaaaalllllllyyy... it would not put in a new line. I guess it just depends on what you need.

j milo taylor

9:21 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



Hmmm, thanksAndrewsmd,

Using the solution you suggested in the whole context of what I'm trying seems to kill the xml output.

FYI here's the whole code. If you could help, I would be so grateful :-()()()()(

<?php

mysql_select_db($database_immapp, $immapp);
$recordID = $_GET['recordID'];
$query_rs_artist_details = "SELECT
(snip)
";
$rs_artist_details = mysql_query($query_rs_artist_details, $immapp) or die(mysql_error());
$totalRows_rs_artist_details = mysql_num_rows($rs_artist_details);

$xml_output = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?> <?xml-stylesheet type=\"text/xsl\" href=\"X3D-XSLT_Artist_Selected_Works.xsl\"?>\n";
$xml_output .= "<artist_works>\n";

for($x =0 ; $x < mysql_num_rows($rs_artist_details) ; $x++){
$row_rs_artist_details = mysql_fetch_assoc($rs_artist_details);
$xml_output .= "<works_details>\n";

$xml_output .= "<Artist_ID>" . $row_rs_artist_details['id_artist'] . "</Artist_ID>\n";
$xml_output .= "<Artist_DOB>" . $row_rs_artist_details['year_artist'] . "</Artist_DOB>\n";
$xml_output .= "<Artist_DOD>" . $row_rs_artist_details['dod_artist'] . "</Artist_DOD>\n";
$xml_output .= "<Artist_First_Name>" . $row_rs_artist_details['1stname_artist'] . "</Artist_First_Name>\n";
$xml_output .= "<Artist_Second_Name>" . $row_rs_artist_details['2ndname_artist'] . "</Artist_Second_Name>\n";
$xml_output .= "<Artist_Image>" . $row_rs_artist_details['image_artist'] . "</Artist_Image>\n";
$xml_output .= "<Work_ID>" . $row_rs_artist_details['id_work'] . "</Work_ID>\n";
$xml_output .= "<Work_Name>" . $row_rs_artist_details['name_work'] . "</Work_Name>\n";
$xml_output .= "<Work_Type>" . $row_rs_artist_details['worktype_work'] . "</Work_Type>\n";
$xml_output .= "<Work_Location>" . $row_rs_artist_details['location_work'] . "</Work_Location>\n";
$xml_output .= "<Work_Exhibited1>" . $row_rs_artist_details['exhibited1_work'] . "</Work_Exhibited1>\n";
$xml_output .= "<Work_Exhibited2>" . $row_rs_artist_details['exhibited2_work'] . "</Work_Exhibited2>\n";
$xml_output .= "<Work_Exhibited3>" . $row_rs_artist_details['exhibited3_work'] . "</Work_Exhibited3>\n";
$xml_output .= "<Work_Exhibited4>" . $row_rs_artist_details['exhibited4_work'] . "</Work_Exhibited4>\n";
$xml_output .= "<Work_Exhibited5>" . $row_rs_artist_details['exhibited5_work'] . "</Work_Exhibited5>\n";
$xml_output .= "<Work_Exhibited6>" . $row_rs_artist_details['exhibited6_work'] . "</Work_Exhibited6>\n";
$xml_output .= "<Work_Image>" . $row_rs_artist_details['image_image'] . "</Work_Image>\n";
$xml_output .= "<Work_Notes>" . returnWord($row_rs_artist_details['notes_work']) . "</Work_Notes>\n";

function returnWord($string){

$count = 1;
$tempArr = split(" ", $string);
foreach($tempArr as $i){

if($count % 8 == 0){
$returnStr .= $i."<br>"." ";
$count = 1;
}//if
else{
$returnStr .= $i." ";
$count++;
}//else

}//foreach
return ($returnStr);
}//returnWord

$xml_output .= "<Work_Audio>" . $row_rs_artist_details['file_audio'] . "</Work_Audio>\n";
$xml_output .= "<Work_Video>" . $row_rs_artist_details['video_video'] . "</Work_Video>\n";

$xml_output .= "</works_details>\n ";
}
$xml_output .= "</artist_works>\n";

$filename = 'XML-ARTIST_SELECTED_WORKS-' . $row_rs_artist_details['1stname_artist'] . $row_rs_artist_details['2ndname_artist'] . '.xml';
if (($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open ($filename)";
exit;
}
if (fwrite($handle, $xml_output) === FALSE) {
echo "Cannot write to ($filename)";
exit;
}
echo "Success, wrote <br> ($xml_output) to ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>

Any thoughts? Cheers, Milo

andrewsmd

9:34 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've never worked with xml output in php. My guess is that the xml output is not strictly a string. Try to parse all of your $rows into individual strings then my function should work. Otherwise, I would need some xml test data to truly test it. Use var_dump on your rows to see what exactly they are to help you better figure out how to make them into a string.

j milo taylor

10:10 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



really thanks for your help.

parsing rows, and var_dump : are beyond my capabilities. :-(

if you can show me what you mean however, i'll give it my best shot

alternatively, here's a sample of the xml i'm working with.

<?xml version="1.0" encoding="iso-8859-1" ?> <?xml-stylesheet type="text/xsl" href="X3D-XSLT_Artist_Selected_Works.xsl"?>
<artist_works>
<works_details>
<Artist_ID>15</Artist_ID>
<Artist_DOB>1950</Artist_DOB>
<Artist_DOD></Artist_DOD>
<Artist_First_Name>Ros</Artist_First_Name>
<Artist_Second_Name>Bandt</Artist_Second_Name>
<Artist_Image>images/artists/Bandt.jpg</Artist_Image>
<Work_ID>70</Work_ID>
<Work_Name>Winds and Circuits, and Surfaces and Cavities</Work_Name>
<Work_Type>Installation</Work_Type>
<Work_Location>Australia</Work_Location>
<Work_Exhibited1>Clifton Hill Community Music Centre</Work_Exhibited1>
<Work_Exhibited2></Work_Exhibited2>
<Work_Exhibited3></Work_Exhibited3>
<Work_Exhibited4></Work_Exhibited4>
<Work_Exhibited5></Work_Exhibited5>
<Work_Exhibited6></Work_Exhibited6>
<Work_Image>images/spacer.jpg</Work_Image>
<Work_Notes> 2 kinetic, electronic and audience participatory sound installations using electrifiedwire coathangers, 8 TV sets, and mazes,</Work_Notes>
<Work_Audio></Work_Audio>
<Work_Video></Work_Video>
</works_details>
<works_details>
<Artist_ID>15</Artist_ID>
<Artist_DOB>1950</Artist_DOB>
<Artist_DOD></Artist_DOD>
<Artist_First_Name>Ros</Artist_First_Name>
<Artist_Second_Name>Bandt</Artist_Second_Name>
<Artist_Image>images/artists/Bandt.jpg</Artist_Image>
<Work_ID>83</Work_ID>
<Work_Name>The Flagong,</Work_Name>
<Work_Type>Instrument Design</Work_Type>
<Work_Location>Australia</Work_Location>
<Work_Exhibited1>The Third Sculpture Triennial</Work_Exhibited1>
<Work_Exhibited2></Work_Exhibited2>
<Work_Exhibited3></Work_Exhibited3>
<Work_Exhibited4></Work_Exhibited4>
<Work_Exhibited5></Work_Exhibited5>
<Work_Exhibited6></Work_Exhibited6>
<Work_Image>images/spacer.jpg</Work_Image>
<Work_Notes>vertical glass marimba, cut bells inspired by Harry Partch, , performances and exhibition</Work_Notes>
<Work_Audio></Work_Audio>
<Work_Video></Work_Video>
</works_details>
<works_details>
<Artist_ID>15</Artist_ID>
<Artist_DOB>1950</Artist_DOB>
<Artist_DOD></Artist_DOD>
<Artist_First_Name>Ros</Artist_First_Name>
<Artist_Second_Name>Bandt</Artist_Second_Name>
<Artist_Image>images/artists/Bandt.jpg</Artist_Image>
<Work_ID>66</Work_ID>
<Work_Name>A Garden for Percy’s Delight</Work_Name>
<Work_Type>Installation</Work_Type>
<Work_Location>Australia</Work_Location>
<Work_Exhibited1></Work_Exhibited1>
<Work_Exhibited2></Work_Exhibited2>
<Work_Exhibited3></Work_Exhibited3>
<Work_Exhibited4></Work_Exhibited4>
<Work_Exhibited5></Work_Exhibited5>
<Work_Exhibited6></Work_Exhibited6>
<Work_Image>images/spacer.jpg</Work_Image>
<Work_Notes>The sounds in this courtyard are derived entirely from recordings I have made from the instruments belonging to Percy Grainger housed in the glass cases in this museum. Some of these sounds have not been heard since Percy gave them to the museum in 1938.
I have designed a floating sound tapestry of these sounds using the Principles of Graingers Free Music, so that every time you come, a different combination will be heard. Grainger designed music with hills and dales, changing fluctuations, curves of high and low, inspired from the wind, the Adelaide Hills and the lapping of water in Albert Park Lake and Brighton Beach. These environmental sounds form a bed over which all other sounds float. The music shifts in relation to itself. Gliding glissando tones and beatless music have been included from archival recordings Percy made. Some of the fascinating sounds you may hear are derived from the following instruments. Accessed 27.04.2007 from [abc.net.au...] </Work_Notes>
<Work_Audio></Work_Audio>
<Work_Video></Work_Video>
</works_details>
</artist_works>

any assistance would help me out enormously.

best regards from london.

bkeep

3:39 am on Aug 15, 2009 (gmt 0)

10+ Year Member



the docs say

If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).

<?php
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);

echo "$newtext\n";
?>

just a guess but setting it to false should keep the word together without a cut.