简体   繁体   English

简单的preg_replace替换锚文本

[英]simple preg_replace replacing anchor text

Having issues wish a preg_replace function. 有问题希望使用preg_replace函数。

I am formatting a breadcrumb which the php is ioncubed so I am having to preg_replace to remove part of the breadcrumb. 我正在格式化将PHP离子化的面包屑,因此我必须preg_replace删除部分面包屑。

I have a breadcrumb that looks like 我有一个看起来像面包屑

<ul>
    <li><a href="mylink1.html">Link 1</a><i class="fa fa-angle-right"></i></li>
    <li><a href="mylink2.html">Link 2</a><i class="fa fa-angle-right"></i></li>
    <li><a href="mylink3.html">Link 3</a></li>
</ul>

What I want to do is completely remove 我想做的是完全删除

<li><a href="#">Link 2</a><i class="fa fa-angle-right"></i></li>

My Original thoughts were to just str_replace 我最初的想法只是str_replace

<li><a href="#">Link 2</a><i class="fa fa-angle-right"></i></li>

with nothing however the link can vary depending on the language file loaded. 没有任何内容,但是链接可以根据加载的语言文件而有所不同。

I then thought about preg_replace the content of the url as the href is constant however I have absolutley no experiance with PHP and all attempts I have tried give random responses. 然后,我想到了preg_replace URL的内容,因为href是恒定的,但是我对PHP没有任何经验,我尝试的所有尝试都给出了随机响应。

I'm hoping someone can help with the preg_replace or even if there is a better way to remove the line. 我希望有人可以提供有关preg_replace的帮助,甚至希望有更好的方法删除该行。

So far Ive tried 到目前为止,我已经尝试过

$str = '<li><a href="mylink.php">Link 2</a></li>';

$preg_replace = preg_replace('<li><a href="mylink.php">(.*)','placeholder',$str);

echo $preg_replace;

Thanks 谢谢

You have to bound your regex, typically with / . 您必须用/绑定正则表达式。 You also might want to include the closing tag or your regex will match more than you want 您可能还想添加结束标记,否则您的正则表达式将比您想要的更匹配

$preg_replace = preg_replace('/<li><a href="mylink\.php">(.*)<\/a>/','placeholder',$str);

I highly recommend http://regex101.com/ for learning more about regex and how to make them work. 我强烈建议http://regex101.com/了解更多关于正则表达式以及如何使它们工作的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM