[英]PHP preg_replace link to get href and anchor text and concatenate it
我需要有關php中某些正則表達式操作的幫助。
我有字符串,例如
$string = 'This article is about something. If You want more
<a href='http://www.google.com'>click here</a>. If not just close
我想得到這樣的結果:
$string = 'This article is about something. If You want more click here -
http://google.com. If not just close
我該如何在php中做到這一點? 我不能使用簡單的HTML解析器和其他庫
請幫助,我不知道如何實現這一目標
真的找不到一步就能完成所有操作的方法,但這應該可行:
<?php
$link = "This article is about something. If You want more <a href='http://www.google.com'>click here</a>. If not just close";
preg_match_all('/<a[^>]+href=([\'"])(.+?)\1[^>]*>/i', $link, $result); // Grab the href
$link = preg_replace("/<a href=.*?>(.*?)<\/a>/",$result[2][0],$link); // remove a tag and replace with href value
echo $link;
// output: This article is about something. If You want more http://www.google.com. If not just close
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.