[pmwiki-users] Category Markup
DaveG
pmwiki at solidgone.com
Sat Dec 17 00:17:58 CST 2005
Patrick R. Michaud wrote:
> On Sat, Dec 17, 2005 at 12:32:09AM -0500, DaveG wrote:
>
>>I'm having trouble with regular expression replacement.
>>
>>Start String : "Tags: a,b, c"
>>Result String: "Tags: [[!a]],[[!b]], [[!c]]"
>>
>>$ROSPatterns["/^Tag(s?):\\s([A-Za-z0-9]\s?)+([,]\s?([A-Za-z0-9]\s?)+)*$/i"]
>>= "Tags: [[!\$2]]";
>
>
> Because of the repeated names on a line I don't think this can
> be done via a regular expression substitution in PHP. AFAIK, a repeated
> regex pattern like (,\s?([A-Za-z0-9]\s?)+)* will capture and replace
> only the last matching repetition.
>
> I think it'll have to be done in two steps: a pattern to recognize
> the Tags: line, and a second one in a subroutine to convert the tags.
> So, perhaps something like:
>
> $ROSPatterns["/^Tags?:\\s*([\\w\\s,]+)/e"] = "TagsToCategories('$1')";
>
> function TagsToCategories($tags) {
> return preg_replace('/\\w+/', '[[!$0]]', $tags);
> }
Your's is way smoother than my solution, based on MediaCat cookbook (I
will learn regex, I will, I will...):
$ROSPatterns["/^Tags?:\\s(.*)/ie"] = "format_Categories('$2')";
function format_Categories($input) {
$categories = preg_split('/, */', $input);
sort($categories);
$label = (count($categories) > 1) ? 'Tags:' : 'Tag:';
if (count($categories)!=0) $input = '[[!'.implode(']], [[!',
$categories).']]';
return '-Tags: '.$input;
}
However, your regex continues past the end of the line, and starts
converting the first word on the next line. I'll work on that tomorrow :)
Thanks for the help,
~ ~ Dave
More information about the pmwiki-users
mailing list