Topic: dealing with URLs again

I am trying to figure out how to take a string that may contain multiple hyperlinks of the form <a href='http://whatever.foo.com/?etc=a&etcetera=3' > and changes them to something of the form  <a href='http://whatever.foo.com/?etc=a&etcetera=3' class='myclass'>. The kicker is that if a hyperlink already has a class='abcoranythingelse'  in it, it is to be left alone. Note that the class= may occur either before or after the href within the <a > element. Also, the attribute values can be found within either single or double quotes.  (I have very little control over what the string contains.)

I have been going at this for quite some time and scrounging things from all over the web that look like they might work with a little modification... so far, nothing seems to work.

Here is a sample of the kind of thing that I have to deal with as the input string:

	<li>
	760-723-2563 &bull; <a    href="mailto:AAAAbbb@xxx.com" >AAAAbbb@xxx.com</a> 
	</li>
</ul>
<ul>
<A  href="http://autrynationalcenterfolkworks.org" class="abc" >http://autrynationalcenter.org</a>	<li>
                   some arbitrary text goes here
<a   class="abc"    href="http://www.artcenter.org/" target="foobar" >http://www.artcenter.org/</a>
                   some arbitrary text goes here
<a     target="foobar"   href="http://www.artcenter.org/"  >http://www.artcenter.org/</a>
                   some arbitrary text goes here
<a     target="foobar"   href="http://www.artcenter.org/">http://www.artcenter.org/</a>
                   some arbitrary text goes here
<A href="http://autrynationalcenterfolkworks.org">http://autrynationalcenter.org</a>	<li>
	AUTRY NATIONAL CENTER 
<a   class='abc'    href="http://www.artcenter.org/" target="foobar" >http://www.artcenter.org/</a>
	</li>
	<li>
	Study of the American West) 
	</li>
	<li>
<a target='foobar' href="http://www.artcenter.org/"  class="abc" >http://www.artcenter.org/</a>
	THE BARCLAY 
	</li>

which needs to get changed to:

	<li>
	760-723-2563 &bull; <a    href="mailto:AAAAbbb@xxx.com" >AAAAbbb@xxx.com</a> 
	</li>
</ul>
<ul>
<A  href="http://autrynationalcenterfolkworks.org" class="abc" >http://autrynationalcenter.org</a>	<li>
                   some arbitrary text goes here
<a   class="abc"    href="http://www.artcenter.org/" target="foobar" >http://www.artcenter.org/</a>
                   some arbitrary text goes here
<a     class='myclass' target="foobar"   href="http://www.artcenter.org/"  >http://www.artcenter.org/</a>
                   some arbitrary text goes here
<a  class='myclass'  target="foobar"   href="http://www.artcenter.org/">http://www.artcenter.org/</a>
                   some arbitrary text goes here
<A  class='myclass' href="http://autrynationalcenterfolkworks.org">http://autrynationalcenter.org</a>	<li>
	AUTRY NATIONAL CENTER 
<a   class='abc'    href="http://www.artcenter.org/" target="foobar" >http://www.artcenter.org/</a>
	</li>
	<li>
	Study of the American West) 
	</li>
	<li>
<a target='foobar' href="http://www.artcenter.org/"  class="abc" >http://www.artcenter.org/</a>
	THE BARCLAY 
	</li>

Re: dealing with URLs again

Would using javascript be out of the question for this?  I know it would be extremely easy to do this with JQuery.  Something like this would work.

<script type="text/javascript">
$(document).ready(function(){
    $('a').each(function(){
        if($(this).attr("class")==""){
            $(this).addClass("newClassName");
        }
    });
});
</script>

Basically it gets every a tag on the page, it it has no class attribute, it adds one.  Otherwise, it's ignored.

Not sure if this suits your needs, this is done on the client side, but it may be easier than dealing with PHP regular expressions.

Last edited by C.Barr (2008-09-10 15:31:28)