Forum Moderators: coopster

Message Too Old, No Replies

Regex Help

         

xKillswitchx

3:36 pm on Feb 9, 2010 (gmt 0)

10+ Year Member



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 (content is grabbed from database), 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.

astupidname

12:28 am on Feb 10, 2010 (gmt 0)

10+ Year Member



Well, if I understand correctly, you are looking for something along the lines of:
$str = 'a string with 2 {gallery=/path/to/images/} paths in it {gallery=/second/path/to/images/} to test with';
$imgStart = '<img src="';
$imgEnd = '" alt="picture" title="">';
//the replacement pattern says:
//find {gallery= then capture everything up til a right brace- '}'
//RegExp "$1" will be the captured () part
$replacedString = preg_replace("/\{gallery=([^\}]+)\}/", $imgStart."$1".$imgEnd, $str);
echo '<br>'.$replacedString;
/*** viewing source will show:
<br>a string with 2 <img src="/path/to/images/" alt="picture" title=""> paths in it <img src="/second/path/to/images/" alt="picture" title=""> to test with
***/

chadhaajay

11:41 am on Feb 16, 2010 (gmt 0)

10+ Year Member



Very nice solution "astupidname". Seems like you work with regular expressions on regular basis.