HTML 使 <p> 元素中的链接可点击,该元素填充了来自 PHP 的文本

我遇到了以下问题:我正在显示一个文本,存储在 mySql 数据库中的一个<p>元素中。当此文本包含 url(例如:https ://google.com/ )时,此 url 不可点击并突出显示。是否有任何解决方案可以突出显示此<p>元素中的 url?



$projectDescription = "Some text..Link1: https://google.com/";


<p class="project-overview-text"><?php echo($projectDescription); ?></p>


慕丝7291255
浏览 176回答 3
3回答

杨__羊羊

您可以尝试以下具有正则表达式并按标签包装 URL 的代码。这适用于任何类型的 URL<?php&nbsp; &nbsp; &nbsp; &nbsp; //Regular Expression to filter urls&nbsp; &nbsp; &nbsp; &nbsp; $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";&nbsp; &nbsp; &nbsp; &nbsp; //your text&nbsp; &nbsp; &nbsp; &nbsp; $projectDescription = "Some text..Link1: https://google.com/";&nbsp; &nbsp; &nbsp; &nbsp; // Checking if any url is present in the text&nbsp; &nbsp; &nbsp; &nbsp; if(preg_match($reg_exUrl, $projectDescription, $url)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Wrap urls by <a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$projectDescription = preg_replace($reg_exUrl, '<a href="'.$url[0].'">'.$url[0].'</a> ', $projectDescription);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; echo $projectDescription;?>

12345678_0001

尝试这个:&nbsp; &nbsp; $projectDescription = "Some text..Link1: https://google.com/";<p class="project-overview-text"><a href="your_link_here"><?php echo($projectDescription); ?></a></p>

至尊宝的传说

尝试这个?<?php$projectDescription = "Some text..Link1: https://google.com/";&nbsp;$position_http = strpos($projectDescription,"http") ;$position_com = strpos($projectDescription,"com") ;$url = substr($projectDescription, $position_http, $position_com+2);?><p class="project-overview-text"><?php echo substr($projectDescription, 0, $position_http);?><a href="<?php echo $url?>" class="project-overview-text"><?php echo($url); ?></a> </p>在此处输入图像描述
打开App,查看更多内容
随时随地看视频慕课网APP