2016-11-04 80 views
-2

我有這個字符串:如何使用PHP從錨的「href」屬性中刪除查詢字符串?

$string = '<a href="/title/tt3110958/?ref_=nm_flmg_act_2" style="color:#666" >'; 

如何從?刪除所有字"在我的字符串。 我試試這個:

$result = preg_replace("/\?.+/", "", $string); 

但其刪除所有字?後!

+1

有一堆不同用PHP做這件事的方法。你有什麼嘗試? –

+0

@JohnConde我更新我的問題! –

+0

使用'/ \?[^「] + /'來代替 – Mohammad

回答

1

使用/\?[^\"]+/像波紋管:

<?php 
$string = '<a href="/title/tt3110958/?ref_=nm_flmg_act_2" style="color:#666" >'; 
$result = preg_replace("/\?[^\"]+/", "", $string); 
echo $result; 
?> 

輸出:<a href="/title/tt3110958/" style="color:#666" >

+0

我用過:'$ result = preg_replace('/ \?[^ \「] + /',」「,$ string);'。 –

0

您可以使用str_replace()

例子:

$myStr = "foo? bar???"; 
$myStr = str_replace("?","\"",$myStr); 
+0

它不會刪除url的查詢字符串。 – Mohammad

+0

我認爲'$ str'是未定義的變量。 –

相關問題