Thread: Regex help?
View Single Post
Old 02-09-2010, 07:58 PM   #3 (permalink)
Killswitch
The Contributor
 
Join Date: Feb 2007
Posts: 64
Thanks: 9
Killswitch is on a distinguished road
Default

Thanks, I will give that a try. I have put regex off for so long, really need to work with it more.

This is the pattern I had, but it currently is half working...

Code:
"/{gallery=(.*)}/i"
It worked on a basic dummy example I had perfectly, but currently it is not working with content pulled from the database (or I am missing something)...

Code:
// The main file
$this->content->data = preg_replace("/{gallery=(.*)}/is", $this->gallery(), $this->data->content);

// gallery() function
$html = "";
		
		if ( ! preg_match("/{gallery=(.*)}/is", $this->data->content, $matches)) return $html;
		
		// Ensure we have trailing /
		if (substr($matches[0], -1) !== '/') $matches[0] .= '/';
		
		// Trim down unwanted chars from match
		$matches[0] = str_replace("{", "", $matches[0]);
		$matches[0] = str_replace("}", "", $matches[0]);
		$matches[0] = str_replace("gallery=", "", $matches[0]);
		
		// Check if the supplied match is a valid directory
		if ( ! is_dir(DOCROOT.$matches[0])) return $html;
		
		// Get files from the supplied dir
		$files = Kohana::list_files(DOCROOT.$matches[0]);
		
		$count = count($files);
		
		if ( ! $count > 0) return $html;
		
		$html = "<div class=\"gallery\">\n";
		foreach ($files AS $file)
		{	
			$sub = substr($file, -3);
			if ($sub != 'jpg' OR $sub != 'png' OR $sub != 'gif') break;
			
			$html .= "<img src=\"$file\">\n";
		}
		$html .= "</div>\n";
		
		return $html;
I know, the gallery() function is a bit messy, I needed to see if it would work, which it doesn't. This entire bit is actually a hook for Kohana which I am using for a CMS I am building with Kohana. (Small note, the returning HTML bit above is just to overwrite the tag)

A small note, the regex you listed above didn't work until I removed the ^ and $ in the pattern.
Killswitch is offline  
Reply With Quote