2015-01-06 35 views
-1

我試圖通過我的URL替換標題之間的-空格,但在echo命令中很難做到這一點。替換回聲中的字符串

字符串:

<a href=\"entry/{$article['id']} {$article['news_title']}\"> 
    {$article['news_title']} 
</a> 

我試圖做:

<a href=\"entry/{$article['id']} 
     .'-' 
     .stripslashes(str_replace(' ', '-', {$article['news_title']})) 
     .'\"> 
    {$article['news_title']} 
</a>  

但它拋出的錯誤。以下是完整的代碼:

echo(" 
     <a href=\"entry/{$article['id']} {$article['news_title']}\"> 
      {$article['news_title']} 
     </a> 
     "); 

回答

1

您需要結束引號,以便您可以調用函數並使用串聯。

echo " 
    <a href=\"entry/{$article['id']} {" . stripslashes(str_replace(' ', '-', $article['news_title'])) . "\">{$article['news_title']}</a>   

    "; 

然而,爲便於閱讀,我建議使用一個變量:

$title_url = stripslashes(str_replace(' ', '-', $article['news_title'])); 
echo(" 
    <a href=\"entry/{$article['id']} {$title_url}\">{$article['news_title']}</a>   

    "); 
0

你有沒有嘗試過這樣的:

<?php 
$url = "entry/".{$article['id']}.{$article['news_title']}; 
$encoded = rawurlencode ($url); 
echo '<a href="'.$encoded.'">'.{$article['news_title']}.'</a>'; 
?>