-2

Looking to do a replace for google index my links without spaces, commas, and another characters

my curent code

<a href="report-<?php echo str_replace(" ", "-", $db['Subject'])?>-<?=$db['id']?>" class="read-more-button">Read More</a>

I'm looking to make it for any characters like $, #, &, ! ; not only for spaces.

4

5 回答 5

0

array您可以像这样向函数发送一个str_replace

$strip = array('%','$','#','&','!'); 
<a href="report-<?=str_replace($strip, '-', $db['Subject'])?>-<?=$db['id']?>" class="read-more-button">Read More</a>

但是,要创建 URL,我使用以下方法:

<?php
    function stripChars($str)
    {
        $bads    =    array(".","+"," ","#","?","!","&" ,"%",":","–","/","\\","'","\"","”","“",",","£","’");
        $goods   =    array("","-","-","-","" ,"" ,"and","" ,"" ,"" ,"","","","","","","","","","");
        $str    =    str_replace($bads,$goods,$str);
        return strtolower($str);
    }
?>
    <a href="<?=stripChars('report ' . $db['Subject'] . ' ' . $db['id'])?>" class="read-more-button">Read More</a>
于 2013-08-09T14:11:38.733 回答
0
<?php
$replace=str_replace(array(" ","\$","#","&","!",";"),'-',$db['Subject']);
echo "<a href='report-{$replace}-{$db['id']}' class='read-more-button'>Read More</a>";
?>

这将创建一个带有 str_replace 的变量 $replace,用连字符将数组中的任何内容

于 2013-08-09T14:11:42.507 回答
0

这就是我用来制造蛞蝓的东西

strtolower(preg_replace('/[\s-]+/', '-', preg_replace('/[^A-Za-z0-9-]+/', '-', preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', trim($string))))));
于 2013-08-09T14:07:52.460 回答
0

str_replace 函数允许您使用数组进行替换。像这样:

首先创建一个要替换的字符数组:

$replace = array(" ", "-", "_", "#", "+", "*");

然后在你的 str_replace 你给它数组名称:

$originalString = '<a href="http://www.somewhere.co.uk">My Link</a>';

$newString = str_replace($replace, "", $originalString);

$newString 将删除 $replace 数组中的每个项目。

于 2013-08-09T14:09:00.467 回答
0

归功于 askbox 作者:

$f = 'fi?le.txt';

$f = str_replace(array('$', '#',' &', '!', '\\','/',':','*','?','"','<','>','|'),' ',$f);

echo $f; // 'fi le.txt'

利用:

function make_safe($str){
return  str_replace(array('$', '#',' &', '!', '\\','/',':','*','?','"','<','>','|'),' ',$str);
}

<a href="report-<?php echo make_safe($db['Subject'])?>-<?=$db['id']?>" class="read-more-button">Read More</a>
于 2013-08-09T14:26:07.617 回答