Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
132973ea
Commit
132973ea
authored
Jan 06, 2017
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract core of qa_q_request into new function, qa_slugify
parent
327a043d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
34 deletions
+63
-34
qa-base.php
qa-include/qa-base.php
+12
-31
string.php
qa-include/util/string.php
+51
-3
No files found.
qa-include/qa-base.php
View file @
132973ea
...
...
@@ -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
)
/*
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
);
}
require_once
QA_INCLUDE_DIR
.
'app/options.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
]);
}
}
require_once
QA_INCLUDE_DIR
.
'app/options.php'
;
require_once
QA_INCLUDE_DIR
.
'util/string.php'
;
$title
=
implode
(
'-'
,
$words
);
if
(
qa_opt
(
'q_urls_remove_accents'
))
$title
=
qa_string_remove_accents
(
$title
);
$title
=
qa_block_words_replace
(
$title
,
qa_get_block_words_preg
());
$slug
=
qa_slugify
(
$title
,
qa_opt
(
'q_urls_remove_accents'
),
qa_opt
(
'q_urls_title_length'
));
return
(
int
)
$questionid
.
'/'
.
$title
;
return
(
int
)
$questionid
.
'/'
.
$slug
;
}
...
...
qa-include/util/string.php
View file @
132973ea
...
...
@@ -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
)
/*
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
);
}
...
...
@@ -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
)
/*
Convert an array of tags into a string for storage in the database
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment