开发者

preg_replace function to append a string to all the hyperlinks of a page

i want to append my own开发者_JAVA技巧 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


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);


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜