Page 1 of 1

Wordpress + Flash = Different Hrefs

Posted: Fri Feb 04, 2011 4:15 pm
by brasofilo
i know that learning regex it's a pain quite worthy, but, please, if some good folk can shortcut me to the solution it'd be very much appreciated...

i'm programming a site with wordpress backend and flash frontend: www.curtaminas.com.br
(google see backend, user see frontend)

so, when i'm reading the database i need to convert normal links into special flash links

if a link contains *.doc, *.zip, *.jpg then i need one output

Code: Select all

<a href="http://www.site.com/uploads/document.doc">Download</a>
must be
<a href="download.php?file=http://www.site.com/uploads/document.doc">Download</a>
else the output is different

Code: Select all

<a href="http://www.site.com/page/this-page">View</a>
must be
<a href="asfunction:openLightbox,http://www.site.com/page/this-page">View</a>


Thankx,
rudolf

Re: Wordpress + Flash = Different Hrefs

Posted: Sat Feb 05, 2011 9:32 am
by brasofilo
well, i came up with a clumsy solution... probably it'd be less memory intensive with preg_replace and regex... but it kinda works

Code: Select all

<?php
$testData = 'text here.......... <a href="http://www.domain.com/something/file.zip">http://www.domain.com/something</a>
text text text text text text text text text text text
<a href="http://www.somelink.com"></a>
text text text text text text text
<a href="http://newswire.com/something/blabla.doc">Some link without WWW</a>
text text text text text text text text text text text text text text
<a title="somename" href="http://www.files.com/download" style="color:#000000" target="_blank">some confusing link</a>
...........more text here..........
<a href="http://www.microsoft.com"></a>
....more text...........
<a href="http://www.site1.com/download.php?file=myfile"><img src="a_link_with_an_image.gif"></a>';

function reformatLinks($string)
{
	$content_array = explode("href=\"", $string);
	$output = '';
	$control = 0;
	foreach($content_array as $content)
	{
		$endpos = stripos($content, "\"")-4;
		$end = substr($content, $endpos, 4);
		
		if($end==".zip" || $end==".doc" || $end==".jpg" || $end==".png" || $end==".pdf" || $end==".xls")
		{
			$content = 'download.php?f=' . $content;
		}
		else
		{
			if($control!=0) $content = 'asfunction:_root.shadowbox,' . $content;
		}
		if($control==0)
		{
			$output .= $content;
			$control++;
		}
		else
		{
			$output .= "href=\"" . $content;
		}
	}
	$output = trim($output);
	return $output;
}
echo reformatLinks($testData);
?>