views:

124

answers:

4

hi guys,

i want to append my own value to all hyperlinks in a page... e.g if there are links:

<a href="abc.htm?val=1">abc 1</a> <br/>
<a href="abc.htm?val=2">abc 1</a> <br/>
<a href="abc.htm?val=3">abc 1</a> <br/>
<a href="abc.htm?val=4">abc 1</a> <br/>

I want to add next var like "type=int" to all hyperlinks

output should be:

<a href="abc.htm?val=1&type=int">abc 1</a> <br/>
<a href="abc.htm?val=2&type=int">abc 1</a> <br/>
<a href="abc.htm?val=3&type=int">abc 1</a> <br/>
<a href="abc.htm?val=4&type=int">abc 1</a> <br/>

I hope it can be done quite easily with preg_replace function

+2  A: 

You can use the output buffer and output_add_rewrite_var to do that. You might need to adjust url_rewriter.tags to only rewrite the href attributes of a elements.

Gumbo
Surely that would affect more than just hyperlinks.
salathe
@salathe: You can adjust the list of tags with *url\_rewriter.tags*.
Gumbo
@Gumbo: Nice of you to mention that in your answer. ;) @KoolKabin: the default tags affected by Gumbo's answer are more wide-ranging than hrefs attributes on hyperlinks.
salathe
A: 

i'm not a regexp-expert, but this sounds like you should think of using sessions or cookies...

oezi
+1  A: 

If it's a matter of just appending the variable to the href attribute, then this would work:

#                                              v-- &amp; instead of & for W3C validation
preg_replace('/(<a\\s+[^>]+href="[^"]*)(")/', '${1}&amp;type=int$2', $page_src);

But if you are wanting code-hardiness, in that href="index.php" gets changed into href="index.php?type=int" and not href="index.php&type=int", then you'll need some extra checking:

function process_link_matches ($matches) {
    # get rid of full matching string; all parts are already covered
    array_shift($matches);

    if (preg_match('/\\?[^"]/', $matches[2])) {
        # link already contains $_GET parameters
        $matches[1] .= '&type=int';
    } else {
        # starting a new list of parameters
        $matches[1] .= '?type=int';
    }

    return implode($matches);
}

$page_src = preg_replace_callback('/(<a\\s+[^>]+href=")([^"]*)("[^>]*>)/', 
    'process_link_matches', $page_src);
amphetamachine
Simple solution was like but u pointed a possible hazard or index.php
KoolKabin
A: 

You can use

preg_replace('/(\<a [^>]*href=")([^"]*)"/i', '&\1\2type=int"', page);

Be warned, though, that this is likely to break on arbitrary html files. I'd recommend using Regex for HTML manipulation only as a one shot operation. If you need to do this more often, or on a lot of files, consider using a HTML parser.

Jens