CodeIgniter Forums
preg_replace / regex - add attributes to link question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: preg_replace / regex - add attributes to link question (/thread-28928.html)



preg_replace / regex - add attributes to link question - El Forum - 03-25-2010

[eluser]BobbyB[/eluser]
Hi,
my urls(in a string) look like this:
Code:
<a href="http://bit.ly/ddGgAi" class="twitter_news_links" target="_blank">http://bit.ly/ddGgAi</a>
I am trying to change them all to:
Code:
<a href="http://bit.ly/ddGgAi" class="twitter_news_links tips" term="http://bit.ly/ddGgAi" target="_blank">http://bit.ly/ddGgAi</a>
So I need to add a class name and the "term" Attribute which is supposed to be the same as "href".
How would you do this?
I have been playing around with regex/preg_replace but couldn't come up with a solution.
Any help is appreciated.


preg_replace / regex - add attributes to link question - El Forum - 03-25-2010

[eluser]danmontgomery[/eluser]
I would use jQuery, personally.

Code:
$('a.twitter_news_links')
    .addClass('tip')
    .attr('term', function() {
        return $(this).attr('href');
    });

Would work, I think (haven't tested).


preg_replace / regex - add attributes to link question - El Forum - 03-25-2010

[eluser]BobbyB[/eluser]
Hi,
you are probably right.
Thanks for sharing.
For Mootools 1.2
Code:
var links_ = $$('a.twitter_news_links');
         links_.each(function(el){
            el.addClass('tips_news');
            el.set('term',el.get('href'));
                    
                      })
Would still be interesting to see a php based solution for this.
Thanks