Click to See Complete Forum and Search --> : [RESOLVED] Decode filename into pretty html link


robertketter
02-10-2008, 08:14 AM
I have a php script parsing an xml document into html links using the file name as the title of the link..
example file name: Sermon20080202 - The Cameleon or the Butterfly.mp3


foreach ($xml->Contents as $Contents) {
echo '<a href="http://www.thedomain.com/'.$Contents->Key.'">'.$Contents->Key.'<a /><br />';

sample html output

<a href="http://www.thedomain.com/Sermon20080202 - The Cameleon or the Butterfly.mp3">
Sermon20080202 - The Cameleon or the Butterfly.mp3
<a /><br />

I would like to clean up the displayed link and am asking for your help...
This is what I would like the link to look like...

<a href="http://www.thedomain.com/Sermon20080202 - The Cameleon or the Butterfly.mp3">
02/02/2008 - The Cameleon or the Butterfly
<a /><br />

Could you break the file name into $date and $title and show me a sample link. I am sure I will be able to work it into my script for a better looking html output.

Thanks so very much.:)

scragar
02-10-2008, 09:23 AM
$x_a = explode(' - ', $Contents->Key);
$x_s = preg_replace("/^.*([0-9]{4})([0-9]{2])([0-9]{2])$/", "{$3}{$2}{$1}",$x_a[0]);
//swap $2 with $3 if wrong order.
echo '<a href="http://www.thedomain.com/'.$Contents->Key."'>{$x_s}</a><br />\n";
edited to remove starting text("Sermon" or whatever).

robertketter
02-10-2008, 10:50 AM
Scragar, thanks so much. I will get a chance later to see how this code works. As always... WebDeveloper....You people are awesome.

scragar
02-10-2008, 12:01 PM
echo '<a href="http://www.thedomain.com/'.$Contents->Key."'>{$x_s}{$x_a[1]}</a><br />\n";
will give you the file name as well, just so you know.

robertketter
02-10-2008, 10:42 PM
I have tried and tried this code but all I get is 500 error and I just cant get it to work. Could someone help me....please.

scragar
02-11-2008, 05:55 AM
replace the preg line with this:
$x_s = preg_replace("/^.*([0-9]+)\/([0-9]+)\/([0-9]+)\$/",
"\\3/\\2/\\1", $x_a[0]);
I was avoiding this first time around because of the forums bad \ repesentation in PHP tags. This code was tested(unlike the previous one that worked in theory).

robertketter
02-11-2008, 08:08 AM
Well, I have done everything I can to make this code work and all it does is remove the "-" dash in the filename and give me the same output. I just dont know why? I had to make a correction in the echo line (change "' to "\") and it began working but as I stated... the output is same except the dash is missing. Any more ideas, I would sure be thankful.

scragar
02-11-2008, 08:14 AM
sorry, I used "02/02/2008 - The Cameleon or the Butterfly" as my testing line instead of the "Sermon20080202 - The Cameleon or the Butterfly.mp3" one...

$x_s = preg_replace("/^.*(\d{4})(\d{2})(\d{2})\$/",
"\\3/\\2/\\1", $x_a[0]);

robertketter
02-11-2008, 08:43 AM
Ok.... now it is working, thank you so very much...:) I really enjoy this website. Whenever I get stuck.... someone always comes to the rescue.