Viewing file: fb2.php (2.91 KB) -rw-rw-r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
getautor();
function getautor(){ $config = simplexml_load_file("prueba.fb2"); //print_r($config); $first_name=$config->{'description'}->{'title-info'}->{'author'}->{'first-name'}; $last_name=$config->{'description'}->{'title-info'}->{'author'}->{'last-name'}; echo $first_name.chr(10); echo $last_name.chr(10); $title=$config->{'description'}->{'title-info'}->{'book-title'}; $lines = file('prueba.fb2'); // lo metemos en un array. echo $inicio_nombre=array_find("<first-name>",$lines); echo $inicio_apellido=array_find("<last-name>",$lines); echo $inicio_titulo=array_find("<book-title>",$lines); echo $fin_nombre=array_find("</first-name>",$lines); echo $fin_apellido=array_find("</last-name>",$lines); echo $fin_titulo=array_find("</book-title>",$lines); // ahora, cambiamos dichos elementos del array $nombre="<first-name>"."hola don pepito"."</first-name>".chr(13).chr(10); $apellido="<last-name>"."se�or lopez"."</last-name>".chr(13).chr(10); $titulo="<book-title>"."Mi Tiotulo"."</book-title>".chr(13).chr(10); $lines[$inicio_nombre]=$nombre; $lines[$inicio_apellido]=$apellido; $lines[$inicio_titulo]=$titulo; // y ahora, creamos de nuevo el ficherito
$handle=fopen("nombrecito.fb2","w+"); foreach ($lines as $key=>$valor){ echo "$key----------$valor".chr(10); fwrite($handle,$valor); } fclose($handle); } function array_find($needle, $haystack) { foreach ($haystack as $key=>$item) { if (strpos($item, $needle) !== FALSE) { return $key; break; } } }
/*
$parent = new DomDocument; $parent_node = $parent ->createElement('author'); $parent_node->appendChild($parent->createElement('first-name', 'nombre del jambo')); $parent_node->appendChild($parent->createElement('last-name', 'apellido del jambo')); $parent->appendChild($parent_node);
$xml = new DOMDocument(); $xml->load('prueba.fb2'); $nodelist=$xml->getElementsByTagName('description/author/title-info');
for ($i = 0; $i < $nodelist->length; $i++) { $innernodelist=$nodelist->item($i)->childNodes; echo $innernodelist->length.chr(10); for($j = 0; $j < $innernodelist->length; $j++){ echo $innernodelist->item($j)->nodeName.chr(10);; if(( (string) $innernodelist->item($j)->nodeName)==='first-name' ){ $replace=$nodelist->item($i); echo $item[i]; print_r($replace); break 2; } } }
if($replace!==null){ $new=$xml->createElement('author'); $nombre=$xml->createElement('first-name','nombre propio'); $apellido=$xml->createElement('last-name','apellido del jambo'); $new->appendChild($nombre); $new->appendChild($apellido);
$replace->parentNode->replaceChild($new,$replace); }
*/
?>
|