Reputation: 1
I would like to use a regular expression to convert links to on page anchors. I am totally new to regex, I have tried learning.
I have used a php string replace to the root url with # but need to use preg_replace to change the / to - but not when / is part of a html tag such as </
or />
.
$string = '<ul>
<li><a href="http://www.somedomain.com/something/somethingelse">Hello World</a></li>
<li><a href="http://www.somedomain.com/something/somethingelse">Hello World</a></li>
</ul>';
// Replace root url with #
$string = str_replace('http://www.somedomain.com/', '#' , $string);
// This replaces the / with hyphens, but I need it to not do this if is preceeded or followed by < or >
$string = preg_replace("/\//", "-", $string);
echo $string;
Upvotes: 0
Views: 87
Reputation: 1481
Kind of:
preg_replace('/([^<]|^)\/([^>]|$)/', '$1-$2', $string);
But the entire task does not look like a reliable way of solving the original task.
Upvotes: 1