开发者

Regex Links to On Page Anchors

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;


Kind of:

preg_replace('/([^<]|^)\/([^>]|$)/', '$1-$2', $string);

But the entire task does not look like a reliable way of solving the original task.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜