Apache2Triad Help, Support and Development Forum Index Apache2Triad Help, Support and Development
The apache2triad help , support and development forums
 

[PHP] preg_replace patterns
Click here to go to the original topic

 
       Apache2Triad Help, Support and Development Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Joshua Meadows (DemoRic)



Joined: 29 Dec 2004
Posts: 785
Location: S.E. Kansas

Posted: Fri Aug 11, 2006 12:13 am    Post subject: [PHP] preg_replace patterns  

I am at a loss here I've been trying to code an rss/xml to xspf converting script for a project and I'm stumped on a pattern.
Code: $patterns[0] = "/<item>/";
   $patterns[1] = "/<\/item>/";
   $patterns[2] = "/<title>/";
   $patterns[3] = "/<\/title>/";
   $patterns[4] = "/<guid>/";
   $patterns[5] = "/<\/guid>/";
   $patterns[6] = "/<enclosure url=/";
   $patterns[7] = "/type='(...)'>/";   

   $replacements[0] = "~new~<track>";
   $replacements[1] = "</track>";
   $replacements[2] = "<annotation>";
   $replacements[3] = "</annotation>";
   $replacements[4] = "<location>";
   $replacements[5] = "</location>";
   $replacements[6] = "<location>";
   $replacements[7] = "</location>";

   ksort($patterns);
   ksort($replacements);
   $fullfile=preg_replace($patterns, $replacements, $fullfile);
The problem I have is with wanting to change:
Quote: <enclosure url="http://site/song.mp3" type="audio/mp3"/>
to
Quote: <location>http://site/song.mp3</location>
Back to top  
cigraphics



Joined: 21 Aug 2005
Posts: 152
Location: Romania, Pitesti

Posted: Fri Aug 11, 2006 9:32 pm    Post subject:  

i think that you want to use something like BBCode:
Code:
<?php
function BBCode($text){
$BBCode = array("<image>" => "<img border=0 src='",
"</image>" => "' />",
"<bold>" => "<b>",
</bold>" => "</b>"
);
$parsedtext = str_replace(array_keys($BBCode), array_values($BBCode), $text);
return $parsedtext;
}
?>
Back to top  
Vlad Alexa Mancini



Joined: 07 Jul 2003
Posts: 1538

Posted: Tue Aug 15, 2006 4:36 pm    Post subject:  

Code: $input = '<enclosure url="http://site/song.mp3" type="audio/mp3"/>';

$output = preg_replace("/(.* url=\")(\S*)(\".*)/","<location>$2</location>",$input);



or

Code: $input = '<enclosure url="http://site/song.mp3" type="audio/mp3"/>';

preg_match("/url=\"(\S*)\"/",$input,$match);

$output = "<location>".$match[1]."</location>";
Back to top  
 
       Apache2Triad Help, Support and Development Forum Index -> Programming
Page 1 of 1


Powered by phpBB Search Engine Indexer
Powered by phpBB 2.0.13 © 2001, 2002 phpBB Group