【问题标题】:Extract text from special link between brackets从括号之间的特殊链接中提取文本
【发布时间】:2021-03-24 21:30:22
【问题描述】:

如何从括号之间的链接中提取文本的链接文本:

例如:

$string = 'adorti sunt Montium primum [a href="https://..."]Link name[/a] divaricaturn sine spiramento ullo. post haec militares, [a href="https://..." class="ln-style-1"]blabla[/a].';

我需要得到:

$result = 'adorti sunt Montium primum Link name divaricaturn sine spiramento ullo. post haec militares, blabla.'

解决方案:

感谢@Slava Rozhnev,我找到了一种方法:

$result = str_replace('[/a]', '', preg_replace('/\[a.*?\]/', '', $string));

【问题讨论】:

标签: php hyperlink extract


【解决方案1】:

你可以像这样使用 PHP preg_replace 函数:

<?php
$string = 'adorti sunt Montium primum [a href="https://..."]Link name[/a] divaricaturn sine spiramento ullo. post haec militares, [a href="https://..." class="ln-style-1"]blabla[/a].';

$result = preg_replace(['/\[a.+\]/Umi','/\[\/a\]/Umi'], '', $string)

echo $result;

PHP code test

【讨论】:

  • 谢谢,谢谢你的回复我找到了办法!您的解决方案是删除整个链接及其名称。
  • 抱歉,误会了。我确定了我的答案。你的会好一点,因为使用一个函数调用
猜你喜欢
  • 2021-05-25
  • 2010-09-27
  • 2014-08-07
  • 2019-02-26
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多