title: php preg_replace pattern - replace text between commas
body: <p>I have a string of words in an array, and I am using <code>preg_replace</code> to make each word into a link. Currently my code works, and each word is transformed into a link.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>$keywords = "shoes,hats,blue curtains,red curtains,tables,kitchen tables";
  
  $template = '&lt;a href="http://domain.com/%1$s"&gt;%1$s&lt;/a&gt;';
  
  $newkeys = preg_replace("/(?!(?:[^&lt;]+&gt;|[^&gt;]+&lt;\/a&gt;))\b([a-z]+)\b/is",    sprintf($template, "\\1"), $keywords);
  </code></pre>
  
  <p>Now, the only problem is that when I want 2 or 3 words to be a single link. For example, I have a keyword "blue curtains". The script would create a link for the word "blue" and "curtains" separately. I have the keywords separated by commas, and I would like the <code>preg_replace</code> to only replace the text between the commas.</p>
  
  <p>I've tried playing around with the pattern, but I just can't figure out what the pattern would be.</p>
  
  <p>Just to clarify, currently the output looks as follows:</p>
  
  <pre><code>  &lt;a href="http://domain.com/shoes"&gt;shoes&lt;/a&gt;,&lt;a     href="http://domain.com/hats"&gt;hats&lt;/a&gt;,&lt;a href="http://domain.com/blue"&gt;blue&lt;/a&gt;     &lt;a href="http://domain.com/curtains"&gt;curtains&lt;/a&gt;,&lt;a     href="http://domain.com/red"&gt;red&lt;/a&gt; &lt;a href="http://domain.com/curtains"&gt;curtains&lt;/a&gt;,&lt;a href="http://domain.com/tables"&gt;tables&lt;/a&gt;,&lt;a href="http://domain.com/kitchen"&gt;kitchen&lt;/a&gt; &lt;a    href="http://domain.com/tables"&gt;tables&lt;/a&gt;
  </code></pre>
  
  <p>While I want to achieve the following output:</p>
  
  <pre><code>&lt;a href="http://domain.com/shoes"&gt;shoes&lt;/a&gt;,&lt;a      href="http://domain.com/hats"&gt;hats&lt;/a&gt;,&lt;a href="http://domain.com/blue      curtains"&gt;blue curtains&lt;/a&gt;,&lt;a href="http://domain.com/red curtains"&gt;red    curtains&lt;/a&gt;,&lt;a href="http://domain.com/tables"&gt;tables&lt;/a&gt;,&lt;a      href="http://domain.com/kitchen tables"&gt;kitchen tables&lt;/a&gt;
  </code></pre>