正则表达式链接到页面锚点
我想使用正则表达式将链接转换为页面锚点。我对正则表达式完全陌生,我尝试过学习。
我使用了 php 字符串替换为 # 的根 url,但需要使用 preg_replace 将 / 更改为 - 但当 / 是 html 标记(例如 或
)的一部分时则不需要>>
。
$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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有点:
但是整个任务看起来不像是解决原始任务的可靠方法。
Kind of:
But the entire task does not look like a reliable way of solving the original task.