1

変数を持つ関数を理解しようとして問題が発生しています。これが私のコードです。詐欺を報告するサイトのわかりやすい URL を作成しようとしています。事前に設定されている場合は、URL から削除する悪い言葉でいっぱいの DB を作成しました。URL の名前にリンクが含まれている場合は、example.com-scam.php または html (どちらか良い方) のようにしたいと思います。ただし、現在は (.) が取り除かれ、この examplecom のようになります。(.) を残して -scam.php または -scam.html を末尾に追加するにはどうすればよいですか?

関数/seourls.php

/* 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);

これを理解する助けがあれば大歓迎です:)また、なぜ関数をエコーし​​なければならないのですか?

4

2 に答える 2

0

コードの最初の実際の行にはコメントがあります。

//make it lowercase, remove punctuation, remove multiple/leading/ending spaces

ピリオドは句読点なので、削除しています。例外を作成する場合は、受け入れられた文字セットに追加.します。

于 2012-10-03T15:15:24.837 に答える
0

正規表現 (2 行目) を変更して、ピリオドを使用できるようにします。

$return = trim(ereg_replace(' +', ' ', preg_replace('/[^a-zA-Z0-9\.\s]/', '', strtolower($link))));

コードをエコーする必要があるのは、関数で変数を返すためです。関数を呼び出してすぐに出力したい場合は、関数内の return を echo/print に変更できます。

于 2012-10-03T15:17:34.050 に答える