Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a little bit tricky, but the following code should work if your URLs are all enclosed in quotation marks (single or double). It will also handle fragment identifiers (like <code>#section-2</code>).</p> <pre><code>$url_modifier = 'utm=some&amp;medium=stuff'; $url_modifier_domain = preg_quote('www.me.com'); $html_text = preg_replace_callback( '#((?:https?:)?//'.$url_modifier_domain.'(/[^\'"\#]*)?)(?=[\'"\#])#i', function($matches){ global $url_modifier; if (!isset($matches[2])) return $matches[1]."/?$url_modifier"; $q = strpos($matches[2],'?'); if ($q===false) return $matches[1]."?$url_modifier"; if ($q==strlen($matches[2])-1) return $matches[1].$url_modifier; return $matches[1]."&amp;$url_modifier"; }, $html_text); </code></pre> <p>Input:</p> <pre><code>&lt;a href="http://www.me.com"&gt;Lorem&lt;/a&gt; &lt;a href="http://www.me.com/"&gt;ipsum&lt;/a&gt; &lt;a href="http://www.me.com/#section-2"&gt;dolor&lt;/a&gt; &lt;a href="http://www.me.com/path-to-somewhere/file.php"&gt;sit&lt;/a&gt; &lt;a href="http://www.me.com/?"&gt;amet&lt;/a&gt;, &lt;a href="http://www.me.com/?foo=bar"&gt;consectetur&lt;/a&gt; &lt;a href="http://www.me.com/?foo=bar#section-3"&gt;elit&lt;/a&gt;. </code></pre> <p>Output:</p> <pre><code>&lt;a href="http://www.me.com/?utm=some&amp;medium=stuff"&gt;Lorem&lt;/a&gt; &lt;a href="http://www.me.com/?utm=some&amp;medium=stuff"&gt;ipsum&lt;/a&gt; &lt;a href="http://www.me.com/?utm=some&amp;medium=stuff#section-2"&gt;dolor&lt;/a&gt; &lt;a href="http://www.me.com/path-to-somewhere/file.php?utm=some&amp;medium=stuff"&gt;sit&lt;/a&gt; &lt;a href="http://www.me.com/?utm=some&amp;medium=stuff"&gt;amet&lt;/a&gt;, &lt;a href="http://www.me.com/?foo=bar&amp;utm=some&amp;medium=stuff"&gt;consectetur&lt;/a&gt; &lt;a href="http://www.me.com/?foo=bar&amp;utm=some&amp;medium=stuff#section-3"&gt;elit&lt;/a&gt;. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload