tags:

views:

131

answers:

4

im having a variable that is containing some links. Now i want to check that which link is clicked and hae to remove its anchor tag so that people can come to know that which tab has been clicked.

this is the example of the data :-

<a href="#" onclick="sort_data('All','all')">All</a> | <a href="#" onclick="sort_data('Diversified','1')">Equity</a> | <a href="#" onclick="sort_data('Liquid','1')">Liquid</a> | <a href="#" onclick="sort_data('Sector','1')">Sector</a>

Now what im trying to do is that i see that which tab is cliked in the js function and then using php to replace that particular anchor tag.But its not running fine.

$links='<a href="#" onclick="sort_data('All','all')">All</a> | <a href="#" onclick="sort_data('Diversified','1')">Equity</a> | <a href="#" onclick="sort_data('Liquid','1')">Liquid</a> | <a href="#" onclick="sort_data('Sector','1')">Sector</a>
';
if(preg_match('/<A HREF="#" onclick="(.*?)>Equity/',$links))
{
    echo preg_replace('/<A HREF=(.*?)>Equity/','Equity',$links);
}

This is replacing everything written before Equity while i want that only Equity's anchor tag should be removed and else should remain as it is. What im doing wrong here and if theres some better way to do this then i would appreciate for telling me.

+1  A: 

you should use the case insensitive flag on preg_match and preg_replace. Also preg_match would match only one link, if you need to replace all link preg_match_all would be better.

$links='<a href="#" onclick="sort_data(\'All\',\'all\')">All</a> | <a href="#" onclick="sort_data(\'Diversified\',\'1\')">Equity</a> | <a href="#" onclick="sort_data(\'Liquid\',\'1\')">Liquid</a> | <a href="#" onclick="sort_data(\'Sector\',\'1\')">Sector</a>
';
if(preg_match('/<A HREF="#" onclick="(.*?)>Equity/i',$links))
{
    echo preg_replace('/<A HREF=([^>]*)>Equity/i','<A $1>Equity',$links);
}


Note: I don't know why you are doing that on server side but might be better to do it on client side with a javascript framework, jquery would be perfect to do such manipulations. Also you have better to make non intrusive javascript that really better for maintenance,

RageZ
this code of urs is still doing the same thing :-( its replacing everything written before equity whereas i want that only equity's anchor tag should be removed.
developer
@developer: sorry I have just edited.
RageZ
@RageZ:How this code of yours will remove anchor tag??? you are simply replacing an anchor tag with another one dude.I want to remove link from equity i.e. i want to replace "<a href="#" onclick="sort_data('Diversified','1')">Equity</a>"withEquity.I hope now my question is clear to you.
developer
+2  A: 

Replace is by default "greedy" and is matching the first "<A HREF..." all the way through to your last "Equity".

Try /<A HREF=([^<>]*?)>Equity/

The [^<>] should restrict your selection to character that are NOT angle brackets and so restrict it to a single href tag.

James Anderson
A: 

If all you want to do is get rid of the <A HREF=...> and </A> tags, why not use the strip_tags function?

MiffTheFox
i dont want to remove all the anchor tags but just a particluar one thats why not using strip_tags.
developer
A: 

You have multiple flaws in your code:

  1. Your searching for upper case letters, although you used lower case letters. To fix this use the i (case insensitive) option or change the reg exp.
  2. You forgot the closing tag </a>.
  3. The greedy * will consume every link in front of the searched one.
  4. (The if branch is possibly unnecessary, if you ony want to continue to use the manipulated string.)

Try this:

$links='<a href="#" onclick="sort_data('All','all')">All</a> | <a href="#" onclick="sort_data('Diversified','1')">Equity</a> | <a href="#" onclick="sort_data('Liquid','1')">Liquid</a> | <a href="#" onclick="sort_data('Sector','1')">Sector</a>';
if(preg_match('/<A([^<>]*)>Equity</A>/i',$links)) {
    echo preg_replace('/<A([^<>]*)>Equity</A>/i','Equity',$links);
}
Christian Strempfer