TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-09-2010, 03:22 AM   #1 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 61
Thanks: 9
Killswitch is on a distinguished road
Default Regex help?

I am working on an app and currently looking to create a plugin to quickly display images from a folder using a quick tag.

I figured in order to do so I would replace ...

{gallery=/path/to/images}

In the content page, replacing it with formatted HTML showing the images in the folder specified.

I am no good with regex (still painfully trying to learn though), and was hoping someone could help me with the match to get the /path/to/images portion of the tag and replacing the entire thing with my HTML.
Killswitch is offline  
Reply With Quote
Old 02-09-2010, 04:45 PM   #2 (permalink)
The Wanderer
 
gamer13's Avatar
 
Join Date: Sep 2009
Posts: 16
Thanks: 4
gamer13 is on a distinguished road
Default

You could use preg_replace_callback() to replace that part with a gallery:

PHP Code:
<?php

function gallery($path_to_images)
{
    
// do something and return it:
    
return;
}

$text 'Hello world!
{gallery=/path/to/images}
Another world!'
;

$new_text preg_replace_callback('/^{gallery=(.*?)}$/''gallery'$text)
Not tested...
gamer13 is offline  
Reply With Quote
Old 02-09-2010, 06:58 PM   #3 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 61
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
Old 02-09-2010, 08:17 PM   #4 (permalink)
The Prestige
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 852
Thanks: 31
delayedinsanity is on a distinguished road
Default

Regular expressions are fairly easy to pick up, they're just a little harder to master.

php Code:
header( 'Content-type: text/plain' );

$string = '<html> <test> Some content {gallery=/path/to/images} some more {gallery} </test> </html>';

// This won't work, because if there's more than one, it will get greedy
// $string = preg_replace( '~{gallery=(.*)}~i', 'FOUND ONE!', $string );

// This only matches up to the first brace it finds
$string = preg_replace( '~{gallery=([^}]+)}~i', 'FOUND ONE!', $string );

echo $string;

The above is just an example of one way to improve your expression, I'll leave the insertion of the gallery to you since you seem to have a handle on it. Uncomment the first one using the original pattern (and comment the second) and you'll see what I'm referring to.

.* is powerful, but almost always misused. Try to avoid it while you're learning and you'll pick up tricks that will help you build much more complex patterns down the road.
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
RegEx ReSpawN Advanced PHP Programming 10 05-10-2008 01:01 PM
Evaluate my regex pleeze? Aaron General 8 05-05-2008 01:24 AM
Majorly Advanced Regex bluesaga Advanced PHP Programming 2 12-12-2007 04:17 PM
RegEx xenon General 6 12-12-2007 09:38 AM
Need Help with RegEx webosb Absolute Beginners 6 12-09-2007 03:17 PM


All times are GMT. The time now is 09:53 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design