PHP搜索引擎优化功能
我在理解带有变量的函数时遇到了问题。这是我的密码。我试图创建一个网站,报告诈骗友好的网址。我创建了一个满是坏单词的数据库,如果它是预设的,可以从url中删除。如果url中的名称包含链接,我希望它看起来像这样:example.com-scam.php或html(以更好的为准)。但是,现在它去掉了(.)并且它看起来像这个示例com。如何修复此问题以离开(.)并在末尾添加-scam.php或-scam.html 函数/seourls.phpPHP搜索引擎优化功能,php,function,Php,Function,我在理解带有变量的函数时遇到了问题。这是我的密码。我试图创建一个网站,报告诈骗友好的网址。我创建了一个满是坏单词的数据库,如果它是预设的,可以从url中删除。如果url中的名称包含链接,我希望它看起来像这样:example.com-scam.php或html(以更好的为准)。但是,现在它去掉了(.)并且它看起来像这个示例com。如何修复此问题以离开(.)并在末尾添加-scam.php或-scam.html 函数/seourls.php /* takes the input, scrubs bad
/* takes the input, scrubs bad characters */
function generate_seo_link($link, $replace = '-', $remove_words = true, $words_array = array()) {
//make it lowercase, remove punctuation, remove multiple/leading/ending spaces
$return = trim(ereg_replace(' +', ' ', preg_replace('/[^a-zA-Z0-9\s]/', '', strtolower($link))));
//remove words, if not helpful to seo
//i like my defaults list in remove_words(), so I wont pass that array
if($remove_words) { $return = remove_words($return, $replace, $words_array); }
//convert the spaces to whatever the user wants
//usually a dash or underscore..
//...then return the value.
return str_replace(' ', $replace, $return);
}
/* takes an input, scrubs unnecessary words */
function remove_words($link,$replace,$words_array = array(),$unique_words = true)
{
//separate all words based on spaces
$input_array = explode(' ',$link);
//create the return array
$return = array();
//loops through words, remove bad words, keep good ones
foreach($input_array as $word)
{
//if it's a word we should add...
if(!in_array($word,$words_array) && ($unique_words ? !in_array($word,$return) : true))
{
$return[] = $word;
}
}
//return good words separated by dashes
return implode($replace,$return);
}
这是我的test.php文件:
require_once "dbConnection.php";
$query = "select * from bad_words";
$result = mysql_query($query);
while ($record = mysql_fetch_assoc($result))
{
$words_array[] = $record['word'];
}
$sql = "SELECT * FROM reported_scams WHERE id=".$_GET['id'];
$rs_result = mysql_query($sql);
while ($row = mysql_fetch_array($rs_result)) {
$link = $row['business'];
}
require_once "functions/seourls.php";
echo generate_seo_link($link, '-', true, $words_array);
如果您能帮助我理解这一点,我们将不胜感激:)另外,我为什么要回显函数?您的第一行实际代码中有这样的注释:
//make it lowercase, remove punctuation, remove multiple/leading/ending spaces
句点是标点符号,所以它们被删除了。如果要例外,请将
添加到接受的字符集。更改正则表达式(第二行)以允许句号:
$return = trim(ereg_replace(' +', ' ', preg_replace('/[^a-zA-Z0-9\.\s]/', '', strtolower($link))));
需要回显代码的原因是,您正在函数中返回一个变量。如果您想在调用函数后立即将其打印出来,可以将函数中的return更改为echo/print。您必须回显函数,因为它返回了一些内容。
ereg
和preg
的随机混合是怎么回事?使用preg
进行所有操作,ereg
将(希望)有一天从php中删除感谢大家的帮助!给你三个问题。如何删除ereg_替换,使其在弃用后仍能正常工作。我不确定它到底在做什么。第二,如何更改函数中的返回,以便在调用时返回?最后,如何将-scam.html添加到字符串的末尾?抱歉,仍在学习:)