codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
<?php $add = array( 'utm_source'=>'edm', 'utm_medium'=>'email', 'utm_campaign'=>'product_notify'); $doc = new DOMDocument(); $doc->loadHTML('First Link <a href="http://www.google.com">no param</a><br /> Second Link <a href="http://www.google.com/">no param with /</a><br /> Third Link <a href="http://www.google.com?query=one">one param</a><br /> Fourth Link <a href="http://www.google.com?query=one&q2=two">two param</a><br />'); foreach($doc->getElementsByTagName('a') as $link){ $url = parse_url($link->getAttribute('href')); $params = array(); if (isset($url['query'])) { parse_str($url['query'], $params); } $gets = array_merge($params, $add); $newstring = ''; if(isset($url['scheme'])) $newstring .= $url['scheme'].'://'; if(isset($url['host'])) $newstring .= $url['host']; if(isset($url['port'])) $newstring .= ':'.$url['port']; if(isset($url['path'])) $newstring .= $url['path']; $newstring .= '?'.http_build_query($gets); if(isset($url['fragment'])) $newstring .= '#'.$url['fragment']; $link->setAttribute('href',$newstring); } $html = $doc->saveHTML(); echo $html;
Private
[
?
]
Run code
Submit