Is it possible to use php to search a string for a file path pattern and then insert a new folder at a specified point?
<?php
$string = '<ul>
<li><a href="/folder1/folder-a/file-a.htm">Link Text a</a></li>
<li><a href="/folder1/folder-b/file-b.htm">Link Text b</a></li>
<li><a href="/folder1/folder-c/file-c.htm">Link Text c</a></li>
<li><a href="/folder1/folder-d/file-d.htm">Link Text d</a></li>
</ul>';
?>
I don’t know in advance what folders-a,b,c,d or files-a,b,c,d will be called so I don’t see how I can use str_replace. I want in each case to insert a new folder before the file so that the outcome would look like this (the new-folder would have the same name in each case):
<?php
$string = '<ul>
<li><a href="/folder1/folder-a/new-folder/file-a.htm">Link Text a</a></li>
<li><a href="/folder1/folder-b/new-folder/file-b.htm">Link Text b</a></li>
<li><a href="/folder1/folder-c/new-folder/file-c.htm">Link Text c</a></li>
<li><a href="/folder1/folder-d/new-folder/file-d.htm">Link Text d</a></li>
</ul>';
?>
Thanks in advance for any feedback.
Assuming your HTML is always well formed this could work for you:
It is, as many people here will emphatically tell you, not a good idea to parse HTML with regex unless you know it to be well formed.