Commit 132973ea by Scott

Extract core of qa_q_request into new function, qa_slugify

parent 327a043d
...@@ -1462,43 +1462,24 @@ ...@@ -1462,43 +1462,24 @@
} }
/**
* Get Q2A request for a question, and make it search-engine friendly, shortening it if necessary
* by removing shorter words which are generally less meaningful.
* @param int $questionid The question ID
* @param string $title The question title
* @return string
*/
function qa_q_request($questionid, $title) function qa_q_request($questionid, $title)
/*
Return the Q2A request for question $questionid, and make it search-engine friendly based on $title, which is
shortened if necessary by removing shorter words which are generally less meaningful.
*/
{ {
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
require_once QA_INCLUDE_DIR.'app/options.php'; require_once QA_INCLUDE_DIR . 'app/options.php';
require_once QA_INCLUDE_DIR.'util/string.php'; require_once QA_INCLUDE_DIR . 'util/string.php';
$title=qa_block_words_replace($title, qa_get_block_words_preg());
$words=qa_string_to_words($title, true, false, false);
$wordlength=array();
foreach ($words as $index => $word)
$wordlength[$index]=qa_strlen($word);
$remaining=qa_opt('q_urls_title_length');
if (array_sum($wordlength)>$remaining) {
arsort($wordlength, SORT_NUMERIC); // sort with longest words first
foreach ($wordlength as $index => $length) {
if ($remaining>0)
$remaining-=$length;
else
unset($words[$index]);
}
}
$title=implode('-', $words); $title = qa_block_words_replace($title, qa_get_block_words_preg());
if (qa_opt('q_urls_remove_accents')) $slug = qa_slugify($title, qa_opt('q_urls_remove_accents'), qa_opt('q_urls_title_length'));
$title=qa_string_remove_accents($title);
return (int)$questionid.'/'.$title; return (int) $questionid . '/' . $slug;
} }
......
...@@ -450,10 +450,12 @@ ...@@ -450,10 +450,12 @@
} }
/**
* Convert accents in a UTF-8 string to ASCII.
* @param string $string Input string.
* @return string
*/
function qa_string_remove_accents($string) function qa_string_remove_accents($string)
/*
Return UTF-8 input $string with all accents (on Roman characters) removed
*/
{ {
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
...@@ -463,6 +465,52 @@ ...@@ -463,6 +465,52 @@
} }
/**
* Convert string to an SEO-friendly URL segment.
* @param string $string Input string.
* @param bool $asciiOnly If true, removes all non-ASCII characters that weren't converted.
* @param int|null $maxLength Maximum length the segment should be, or null for no limit.
* @return string
*/
function qa_slugify($string, $asciiOnly=true, $maxLength=null)
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
$words = qa_string_to_words($string, true, false, false);
if ($maxLength !== null) {
$wordlength = array();
foreach ($words as $index => $word) {
$wordlength[$index] = qa_strlen($word);
}
$remaining = $maxLength;
if (array_sum($wordlength)>$remaining) {
arsort($wordlength, SORT_NUMERIC); // sort with longest words first
foreach ($wordlength as $index => $length) {
if ($remaining > 0) {
$remaining -= $length;
} else {
unset($words[$index]);
}
}
}
}
$string = implode('-', $words);
if ($asciiOnly) {
// convert accents to ASCII, then remove all remaining non-ASCII characters
$string = qa_string_remove_accents($string);
$string = preg_replace('/[^ a-zA-Z0-9-]/', '', $string);
$string = preg_replace('/-{2,}/', '-', trim($string, '-'));
}
return $string;
}
function qa_tags_to_tagstring($tags) function qa_tags_to_tagstring($tags)
/* /*
Convert an array of tags into a string for storage in the database Convert an array of tags into a string for storage in the database
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment