How to remove everything except strings surrounded by <![CDATA[ and ]]> ?

Discussion in 'PHP' started by lelkoun, Jun 6, 2011.

  1. #1
    Original code:

    <phrase name="ascending" date="1287942994" username="Správce" version="4.0.8"><![CDATA[[B]Vzestupně[/B]]]></phrase>
    <phrase name="descending" date="1287942982" username="Správce" version="4.0.8"><![CDATA[[B]Sestupně[/B]]]></phrase>
    <phrase name="reminder" date="1287942945" username="Správce" version="4.0.8"><![CDATA[[B]Jméno[/B]]]></phrase>
    Code (markup):
    I want to get this:
    VzestupnÄ›
    SestupnÄ›
    Jméno
    Code (markup):
    Do you know how to achieve this?
    Thanks.
     
    lelkoun, Jun 6, 2011 IP
  2. SiJz

    SiJz Peon

    Messages:
    51
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,
    No problem use this little bit of script:

    
    <?php
    $Din = file('./textfile.txt');		// get textfile contents
    $Dout = fopen("./textout.txt", "w");	// set output file
    
    $lines = array_unique($Din);			// split into individual lines
    
    $STARTtag = "<![CDATA[";
    $ENDtag = "]]>";
    
    foreach($lines as $line) 
    {
    	$line = substr($line, (strpos($line, $STARTtag) + strlen($STARTtag)) );	// strip to and including start tag
    	$line = substr($line, 0, strpos($line, $ENDtag));				// strip from and including end tag
    	echo($line."<br>");	// for browser output if required
    	$line.= "\r\n";		// add line termination for output file
    	fputs($Dout, $line);	// write to file
    }
    
    fclose($Dout);			// close output file
    
    ?>
    
    Code (markup):
    my textfile.txt had the following content
    
    <phrase name="ascending" date="1287942994" username="Správce" version="4.0.8"><![CDATA[Vzestupne]]></phrase>
    <phrase name="descending" date="1287942982" username="Správce" version="4.0.8"><![CDATA[Sestupne]]></phrase>
    <phrase name="reminder" date="1287942945" username="Správce" version="4.0.8"><![CDATA[Jméno]]></phrase>
    
    Code (markup):
    (I lost some characters when copying via notepad, not due to the script)

    which gives the output text file contents of:
    Vzestupne
    Sestupne
    Jméno


    Hope that helps?

    Si
     
    SiJz, Jun 6, 2011 IP
    lelkoun likes this.
  3. lelkoun

    lelkoun Active Member

    Messages:
    288
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thank you very much! :)
     
    lelkoun, Jun 6, 2011 IP
  4. SiJz

    SiJz Peon

    Messages:
    51
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No Problem, happy to help

    Si
     
    SiJz, Jun 6, 2011 IP