Markup:Regex

Creating and modifying plugins.
Post Reply
Ansgar
Regular
Posts: 28
Joined: Wed Apr 15, 2009 8:42 pm

Markup:Regex

Post by Ansgar »

Hiho!

I want to use the Regex-Plugin zu change the link-syntax in the articles.

A link like this

Code: Select all

<a href="http://board.s9y.org">s9y Forum</a>
should be changed into this

Code: Select all

<a href="http://board.s9y.org" target="_blank">s9y Forum</a>
and this should work for Links with title-tags too.

I´m trying to understand and adapt the ClickableURL example, but as I'm new to regular expression and the Plugin, I don`t get it...

Are the items in the SearchArray alternatives that is searched for?
What do the numbers in the Replace Array represent? \\1 \\2 \\3 \\4

Regards
Ansgar
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Markup:Regex

Post by garvinhicking »

Hi!

A rule like

Code: Select all

$regexpArray = array(
    'SearchArray'=>array(
      '@<a ([^>]+)>@i',
    ),
    'ReplaceArray'=>array(
      '<a target="_blank" \\1>'
    )
);
should do the trick. When you already use "target=_blank" somewhere, you'd get it a second time. Working around that is pretty tricky with RegExps, but having two attributes would work in every browser still. ;)
Are the items in the SearchArray alternatives that is searched for?
SearchArray can contain as many patterns as you might want to use, so a single PHP-File could take care of multiple replacements.
What do the numbers in the Replace Array represent? \\1 \\2 \\3 \\4
Those are placeholdes for strings that have been matched with (...) in the searchArray, which can then be inserted into the string.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Ansgar
Regular
Posts: 28
Joined: Wed Apr 15, 2009 8:42 pm

Re: Markup:Regex

Post by Ansgar »

Hiho Garvin,

thank a lot!

meanwhile I've found another working solution:

Code: Select all

$regexpArray = array(
    'SearchArray'=>array(
    // a well formatted link with no extra attributes
		"/<a href=\"([^\"]*)\">(.*)<\/a>/iU"
    ),
    'ReplaceArray'=>array(
		'<a href="\\1" target="_blank">\\2</a>'
    )
); 
Which one is better? ;)
SearchArray can contain as many patterns as you might want to use, so a single PHP-File could take care of multiple replacements.
so the first item in SearchArray is replaced by the first item in ReplaceArray?

... and the multiple files in serendipy_event_regexp/regexps or for clearness only?

Best regards,
Ansgar
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Markup:Regex

Post by garvinhicking »

Hi!

This one is more problematic, if you have a link <a title="mylink" href="..."> then it will not be rewriten. My version transfers every <a.*>-Tag...
... and the multiple files in serendipy_event_regexp/regexps or for clearness only?
They are seperated so that you can selectively delete some files if you don't want that specific markup.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Ansgar
Regular
Posts: 28
Joined: Wed Apr 15, 2009 8:42 pm

Re: Markup:Regex

Post by Ansgar »

Hiho!

Ok, then I'll use your version :)

Thanks a lot for your help!

Best Regards,
Ansgar
Post Reply