title: PHP pattern search and replace
body: <p>I have a bunch of rawr contents in database.</p>
  
  <p>some containing string <code>http://www.example.com/subfolder/name.pdf</code> or <code>/subfolder/name.pdf</code></p>
  
  <p>I need a pattern replace on these to turn them into <code>/wp-content/uploads/old/subfolder/name.pdf</code> there can be many levels of subfolders!
  <code>/subfolder1/subfolder2/subfolder3/file.pdf</code></p>
  
  <p>The pattern for finding I use is </p>
  
  <pre><code>/http[^\s]+pdf/
  /href="\/[^\s]+pdf/
  </code></pre>
  
  <p>But how to replace the pattern with another pattern? ( the example above ^ )</p>
  
  <p>I have </p>
  
  <pre><code>search for /http:\/\/www.example.com(.*).pdf"/
  replace with /wp-content/uploads/old$1.pdf"
  
  search for /href="\/pdf(.*)\.pdf"&gt;/
  </code></pre>
  
  <p>this works fine until there are more than 1 pdf links in one table cell</p>
  
  <p>example</p>
  
  <p><code>&lt;a href="/pdf/subdir/name.pdf"&gt;clickhere&lt;/a&gt;&lt;a href="/pdf/subdir/name.pdf"&gt;2nd PDF&lt;/a&gt;</code></p>