Forum Moderators: coopster
i.e. <a href="..." class="one">one</a> or it could be <a href="..." class="two">two</a>.
Basically I want to change the class="..." to class="myclass".
I am finding this difficult because of the random attribute value. Can somebody help please?
I want to be able to scan a paragraph of text and pick out a class on a link (<a class='anything'>), and change the class value to my own class value i.e. <a class='anything'> would become <a class='myclass'>. The issue is that the value I want to change is different each time, so it could be <a class='something'> instead of <a class='anything'>. I also want to ignore the other link attributes such as the href attribute.
Hope this clarifies for you :)
[edited by: eelixduppy at 8:00 pm (utc) on Mar. 20, 2008]
[edit reason] disabled smileys [/edit]
$pattern = "/(<[^>]+class=)\"([^\"]*)\"/i";
$str = preg_replace('/class=\"([^\"]*)\"/i', 'class=\'myclass\'', $str);
This script works well, but does not perform the replace inside an <a> tag. Would you be able to assist with this?
Also, I am intending to implement a similar version which ensures target="" attributes are set to target="_blank". This is easy, but like the class example needs to cater for both single (') and double (") quotes, and be inside a <a> tag. Can this be incorporated into my script?
My final question is, when doing the target="..." preg_replace, is there a means of determining whether a target is specified on a link, and if not, add target="_blank"?
Basically I am trying to create simple, clean links, adhering to the following:
<a href="..." class="myclass" target="_blank">...</a>
Thanks for your help thus far.