Forum Moderators: coopster

Message Too Old, No Replies

preg_replace issue

         

username

3:51 am on Mar 20, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month




System: The following message was cut out of thread at: http://www.webmasterworld.com/forum88/7125.htm [webmasterworld.com] by encyclo - 9:14 am on Mar. 20, 2008 (utc -4)


Hi everyone. I have a preg_replace issue I hope somecan help with. I want to replace a class declaration in a link with my own class. The catch is, that the class value is random and will vary depending on the content available.

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?

coopster

3:55 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, username.

You want to change the value of the class attribute? To what value? The text node of the link itself, the text between the <a> tags?

username

6:17 pm on Mar 20, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi coopster.

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]

coopster

7:30 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure does, thanks. This one is free, the next one I'll make you give it your best attempt first, all right? ;)
$pattern = "/(<[^>]+class=)\"([^\"]*)\"/i";

It says to match any opening element marker (the less than sign) followed by 1 or more of anything that is not a closing element marker followed by a class attribute whose value is contained in double quotation marks. And the "i" says make it case-insensitive. This replaces any and all classes. If you want it to be specific to <a> elements, you will need to modify the pattern. It should get you started though.

username

4:02 am on Mar 21, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi Coopster. The script you provided seemed to strip out everything including the tag, only leaving the class attribute...so I refined it to the following:

$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.