PHP:从链接标题中删除`http://` [英] PHP: remove `http://` from link title

查看:81
本文介绍了PHP:从链接标题中删除`http://`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如下所示:

$ string ='< a href =http://google.com > http://google.com< / a>';



如何删除 http: // 部分来自链接文本,但将其保留在href属性中?

解决方案

一个完整的解析器,这可能会在大多数情况下... ...

  $ str ='< a href = http://google.com> HTTP://google.com< / A>'; 

$ regex ='/(?<!href = [\'])http:\ / \\\ //';

$ str = preg_replace ($ regex,'',$ str);

var_dump($ str); // string(42)< a href =http://google.com> google。 com< / a>

它使用负面lookbehind 在它之前没有 href = href ='



在IDEone上查看



它还会考虑使用'分隔其属性值的人员。


I have a string that looks like:

$string = '<a href="http://google.com">http://google.com</a>';

How can I remove the http:// part from the link text, but leave it in the href attribute?

解决方案

Without using a full blown parser, this may do the trick for most situations...

$str = '<a href="http://google.com">http://google.com</a>';

$regex = '/(?<!href=["\'])http:\/\//';

$str = preg_replace($regex, '', $str);

var_dump($str); // string(42) "<a href="http://google.com">google.com</a>"

It uses a negative lookbehind to make sure there is no href=" or href=' preceding it.

See it on IDEone.

It also takes into account people who delimit their attribute values with '.

这篇关于PHP:从链接标题中删除`http://`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆