用PHP重新格式化字符串中的链接

用PHP重新格式化字符串中的链接,php,string,html-parsing,Php,String,Html Parsing,我有一些链接文本。我想重新格式化文本,使每个url出现在其相应链接的名称之后,并用括号括起来,所有标记都被删除(我将其写入CSV) 那么比如说, <a href="http://test.com">TestWebsite1</a> 我认为这种方法有点乏味: get index of each occurrence of "<a" use regex to get all text following that up to next occurrence of "&

我有一些链接文本。我想重新格式化文本,使每个url出现在其相应链接的名称之后,并用括号括起来,所有标记都被删除(我将其写入CSV)

那么比如说,

<a href="http://test.com">TestWebsite1</a>
我认为这种方法有点乏味:

get index of each occurrence of "<a"
use regex to get all text following that up to next occurrence of ">"
find next occurrence of <
insert text at that index 
str_replace "<a href=“ with "(" 
etc 
获取“”的每次出现的索引
查找下一个事件<
在该索引处插入文本

str_replace“您也许应该看看markdown converter,并使用这种格式


您可能应该看看markdown converter。只需使用该格式即可


请使用合适的HTML解析器:

$html = <<<HTML
<a href="http://test.com">TestWebsite1</a>
HTML;

$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//a[@href]') as $anchor) {
    printf("%s (%s)\n", $anchor->textContent, $anchor->getAttribute('href'));
}
$html=textContent,$anchor->getAttribute('href');
}

请使用合适的HTML解析器:

$html = <<<HTML
<a href="http://test.com">TestWebsite1</a>
HTML;

$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//a[@href]') as $anchor) {
    printf("%s (%s)\n", $anchor->textContent, $anchor->getAttribute('href'));
}
$html=textContent,$anchor->getAttribute('href');
}

是的,有,它被称为DOM..regex+html导致人类牺牲,狗和猫生活在一起,集体歇斯底里…-看看例子。是的,有,它被称为DOM..regex+html导致人类牺牲,狗和猫生活在一起,集体歇斯底里…-看看例子。