for ($i = 0; $i <count($rawtags); $i++){
$tags = '<tag value="' . $rawtags[$i] . '" />';
}
당신에게 여기
<?php
function gettags($xml)
{
$xmltags = $xml->xpath('//var[@name="infocodes"]/string');
return $xmltags[0];
}
//Path to the XML files on the server
$path = "/xmlfiles/";
//Create an array with all the XML files
$files = glob("$path/*.xml");
foreach($files as $file)
{
$xml = simplexml_load_file($file);
$xmltags = gettags($xml);
//Using the , character split the values coming from the $xmltags into an array
$rawtags = explode(',', $xmltags);
//Loop through the tags and add to a variable. Each tag will be inside an XML element - <tag value="tagname" />
for ($i = 0; $i <count($rawtags); $i++){
$tags = '<tag value="' . $rawtags[$i] . '" />';
}
//Replace HTML escaped characters (ä, å, ö, Å, Ä, Ö) and the | character with normal characters in the tags variable
$tagsunwantedchars = array("Ö", "Ä", "Å", "ö", "ä", "å", "|");
$tagsreplacewith = array("Ö", "Ä", "Å", "ö", "ä", "å", " - ");
$tagsclean = str_replace($tagsunwantedchars, $tagsreplacewith, $tags);
//Create the full tag list and store in a variable
$taglist = "<tags>$tagsclean</tags>";
}
echo $taglist;
?>
XML 파일입니다 : 여기
코드입니다 방금 끝났어. 모든 반복마다
$tags
변수를 쓰십시오. 보십시오 : 당신이 그것에 추가하기 전에
$tags
를 초기화해야
$tags = '';
foreach ($rawtags as $rawtag) {
$tags .= '<tag value="' . $rawtag . '" />';
}
주 (그렇지 않으면이 PHP 경고를 생성합니다).
또한 for
루프 대신 foreach
을 사용하면 코드를 더 간단하고 읽기 쉽게 만들 수 있습니다. 이와 같은 사소한 오류는 소음으로 둘러싸여 있지 않은 경우 더 쉽게 발견 할 수 있습니다.
이 항목에 걸린 시간은 얼마입니까? – RMcLeod
와우 같은 쉬운 수정! 고마워. – Brigante
또는'$ tags [] = ...'를 사용하고 루프 다음에'implode'을 사용하십시오. 좀 더 공연해야합니다. – outis