TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Regex help? (http://www.talkphp.com/general/5262-regex-help.html)

Killswitch 02-09-2010 04:22 AM

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.

gamer13 02-09-2010 05:45 PM

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...

Killswitch 02-09-2010 07:58 PM

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.

delayedinsanity 02-09-2010 09:17 PM

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.


All times are GMT. The time now is 05:25 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0