2015-01-06 32 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>'; 
?>