PHP:从链接标题中删除`http://`

Ale*_*lex 8 html php string

我有一个看起来像这样的字符串:

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

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

ale*_*lex 11

如果不使用完整的解析器,这可能适用于大多数情况......

$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>"
Run Code Online (Sandbox Code Playgroud)

它使用负面的lookbehind来确保它没有href="或在href='它之前.

在IDEone上看到它.

它还考虑了用其分隔属性值的人'.


Nei*_*ght 9

$string = '<a href="http://google.com">http://google.com</a>'; 
$var = str_replace('>http://','>',$string); 
Run Code Online (Sandbox Code Playgroud)

刚刚在IDEone.com中试过它,它具有所需的效果.

  • 值得扔掉那里,这不会抓住`> http:// ...`,但是如果你事先修剪掉空格,那就应该这样做. (2认同)