/**
* Return a friendly url made from the provided string
* If the mbstring library is available, the output is the same as the js function of the same name
*
* @param string $str
* @return string
*/
public static function str2url($str)
{
static $allow_accented_chars = null;
if ($allow_accented_chars === null)
$allow_accented_chars = Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL');
$str = trim($str);
if (function_exists('mb_strtolower'))
$str = mb_strtolower($str, 'utf-8');
if (!$allow_accented_chars)
$str = Tools::replaceAccentedChars($str);
// Remove all non-whitelist chars.
if ($allow_accented_chars)
$str = preg_replace('/[^a-zA-Z0-9\s\'\:\/\[\]-\pL]/u', '', $str);
else
$str = preg_replace('/[^a-zA-Z0-9\s\'\:\/\[\]-]/','', $str);
$str = preg_replace('/[\s\'\:\/\[\]\-]+/', ' ', $str);
$str = str_replace(array(' ', '/'), '-', $str);
// If it was not possible to lowercase the string with mb_strtolower, we do it after the transformations.
// This way we lose fewer special chars.
if (!function_exists('mb_strtolower'))
$str = Tools::strtolower($str);
return $str;
}
/**
* Replace all accented chars by their equivalent non accented chars.
*
* @param string $str
* @return string
*/
public static function replaceAccentedChars($str)
{
/* One source among others:
http://www.tachyonsoft.com/uc0000.htm
http://www.tachyonsoft.com/uc0001.htm
http://www.tachyonsoft.com/uc0004.htm
*/
$patterns = array(
/* Lowercase */
/* a */ '/[\x{00E0}\x{00E1}\x{00E2}\x{00E3}\x{00E4}\x{00E5}\x{0101}\x{0103}\x{0105}\x{0430}]/u',
/* b */ '/[\x{0431}]/u',
/* c */ '/[\x{00E7}\x{0107}\x{0109}\x{010D}\x{0446}]/u',
/* d */ '/[\x{010F}\x{0111}\x{0434}]/u',
/* e */ '/[\x{00E8}\x{00E9}\x{00EA}\x{00EB}\x{0113}\x{0115}\x{0117}\x{0119}\x{011B}\x{0435}\x{044D}]/u',
/* f */ '/[\x{0444}]/u',
/* g */ '/[\x{011F}\x{0121}\x{0123}\x{0433}\x{0491}]/u',
/* h */ '/[\x{0125}\x{0127}]/u',
/* i */ '/[\x{00EC}\x{00ED}\x{00EE}\x{00EF}\x{0129}\x{012B}\x{012D}\x{012F}\x{0131}\x{0438}\x{0456}]/u',
/* j */ '/[\x{0135}\x{0439}]/u',
/* k */ '/[\x{0137}\x{0138}\x{043A}]/u',
/* l */ '/[\x{013A}\x{013C}\x{013E}\x{0140}\x{0142}\x{043B}]/u',
/* m */ '/[\x{043C}]/u',
/* n */ '/[\x{00F1}\x{0144}\x{0146}\x{0148}\x{0149}\x{014B}\x{043D}]/u',
/* o */ '/[\x{00F2}\x{00F3}\x{00F4}\x{00F5}\x{00F6}\x{00F8}\x{014D}\x{014F}\x{0151}\x{043E}]/u',
/* p */ '/[\x{043F}]/u',
/* r */ '/[\x{0155}\x{0157}\x{0159}\x{0440}]/u',
/* s */ '/[\x{015B}\x{015D}\x{015F}\x{0161}\x{0441}]/u',
/* ss */ '/[\x{00DF}]/u',
/* t */ '/[\x{0163}\x{0165}\x{0167}\x{0442}]/u',
/* u */ '/[\x{00F9}\x{00FA}\x{00FB}\x{00FC}\x{0169}\x{016B}\x{016D}\x{016F}\x{0171}\x{0173}\x{0443}]/u',
/* v */ '/[\x{0432}]/u',
/* w */ '/[\x{0175}]/u',
/* y */ '/[\x{00FF}\x{0177}\x{00FD}\x{044B}]/u',
/* z */ '/[\x{017A}\x{017C}\x{017E}\x{0437}]/u',
/* ae */ '/[\x{00E6}]/u',
/* ch */ '/[\x{0447}]/u',
/* kh */ '/[\x{0445}]/u',
/* oe */ '/[\x{0153}]/u',
/* sh */ '/[\x{0448}]/u',
/* shh*/ '/[\x{0449}]/u',
/* ya */ '/[\x{044F}]/u',
/* ye */ '/[\x{0454}]/u',
/* yi */ '/[\x{0457}]/u',
/* yo */ '/[\x{0451}]/u',
/* yu */ '/[\x{044E}]/u',
/* zh */ '/[\x{0436}]/u',
/* Uppercase */
/* A */ '/[\x{0100}\x{0102}\x{0104}\x{00C0}\x{00C1}\x{00C2}\x{00C3}\x{00C4}\x{00C5}\x{0410}]/u',
/* B */ '/[\x{0411}]]/u',
/* C */ '/[\x{00C7}\x{0106}\x{0108}\x{010A}\x{010C}\x{0426}]/u',
/* D */ '/[\x{010E}\x{0110}\x{0414}]/u',
/* E */ '/[\x{00C8}\x{00C9}\x{00CA}\x{00CB}\x{0112}\x{0114}\x{0116}\x{0118}\x{011A}\x{0415}\x{042D}]/u',
/* F */ '/[\x{0424}]/u',
/* G */ '/[\x{011C}\x{011E}\x{0120}\x{0122}\x{0413}\x{0490}]/u',
/* H */ '/[\x{0124}\x{0126}]/u',
/* I */ '/[\x{0128}\x{012A}\x{012C}\x{012E}\x{0130}\x{0418}\x{0406}]/u',
/* J */ '/[\x{0134}\x{0419}]/u',
/* K */ '/[\x{0136}\x{041A}]/u',
/* L */ '/[\x{0139}\x{013B}\x{013D}\x{0139}\x{0141}\x{041B}]/u',
/* M */ '/[\x{041C}]/u',
/* N */ '/[\x{00D1}\x{0143}\x{0145}\x{0147}\x{014A}\x{041D}]/u',
/* O */ '/[\x{00D3}\x{014C}\x{014E}\x{0150}\x{041E}]/u',
/* P */ '/[\x{041F}]/u',
/* R */ '/[\x{0154}\x{0156}\x{0158}\x{0420}]/u',
/* S */ '/[\x{015A}\x{015C}\x{015E}\x{0160}\x{0421}]/u',
/* T */ '/[\x{0162}\x{0164}\x{0166}\x{0422}]/u',
/* U */ '/[\x{00D9}\x{00DA}\x{00DB}\x{00DC}\x{0168}\x{016A}\x{016C}\x{016E}\x{0170}\x{0172}\x{0423}]/u',
/* V */ '/[\x{0412}]/u',
/* W */ '/[\x{0174}]/u',
/* Y */ '/[\x{0176}\x{042B}]/u',
/* Z */ '/[\x{0179}\x{017B}\x{017D}\x{0417}]/u',
/* AE */ '/[\x{00C6}]/u',
/* CH */ '/[\x{0427}]/u',
/* KH */ '/[\x{0425}]/u',
/* OE */ '/[\x{0152}]/u',
/* SH */ '/[\x{0428}]/u',
/* SHH*/ '/[\x{0429}]/u',
/* YA */ '/[\x{042F}]/u',
/* YE */ '/[\x{0404}]/u',
/* YI */ '/[\x{0407}]/u',
/* YO */ '/[\x{0401}]/u',
/* YU */ '/[\x{042E}]/u',
/* ZH */ '/[\x{0416}]/u');
// ö to oe
// å to aa
// ä to ae
$replacements = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 'ss', 't', 'u', 'v', 'w', 'y', 'z', 'ae', 'ch', 'kh', 'oe', 'sh', 'shh', 'ya', 'ye', 'yi', 'yo', 'yu', 'zh',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'Y', 'Z', 'AE', 'CH', 'KH', 'OE', 'SH', 'SHH', 'YA', 'YE', 'YI', 'YO', 'YU', 'ZH'
);
return preg_replace($patterns, $replacements, $str);
}
/**
* Truncate strings
*
* @param string $str
* @param integer $max_length Max length
* @param string $suffix Suffix optional
* @return string $str truncated
*/
/* CAUTION : Use it only on module hookEvents.
** For other purposes use the smarty function instead */
public static function truncate($str, $max_length, $suffix = '...')
{
if (Tools::strlen($str) <= $max_length)
return $str;
$str = utf8_decode($str);
return (utf8_encode(substr($str, 0, $max_length - Tools::strlen($suffix)).$suffix));
}
/*Copied from CakePHP String utility file*/
public static function truncateString($text, $length = 120, $options = array())
{
$default = array(
'ellipsis' => '...', 'exact' => true, 'html' => true
);
$options = array_merge($default, $options);
extract($options);
if ($html)
{
if (Tools::strlen(preg_replace('/<.*?>/', '', $text)) <= $length)
return $text;
$totalLength = Tools::strlen(strip_tags($ellipsis));
$openTags = array();
$truncate = '';
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
foreach ($tags as $tag)
{
if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2]))
{
if (preg_match('/<[\w]+[^>]*>/s', $tag[0]))
array_unshift($openTags, $tag[2]);
elseif (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag))
{
$pos = array_search($closeTag[1], $openTags);
if ($pos !== false)
array_splice($openTags, $pos, 1);
}
}
$truncate .= $tag[1];
$contentLength = Tools::strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
if ($contentLength + $totalLength > $length)
{
$left = $length - $totalLength;
$entitiesLength = 0;
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE))
{
foreach ($entities[0] as $entity)
{
if ($entity[1] + 1 - $entitiesLength <= $left)
{
$left--;
$entitiesLength += Tools::strlen($entity[0]);
}
else
break;
}
}
$truncate .= Tools::substr($tag[3], 0, $left + $entitiesLength);
break;
}
else
{
$truncate .= $tag[3];
$totalLength += $contentLength;
}
if ($totalLength >= $length)
break;
}
}
else
{
if (Tools::strlen($text) <= $length)
return $text;
$truncate = Tools::substr($text, 0, $length - Tools::strlen($ellipsis));
}
if (!$exact)
{
$spacepos = Tools::strrpos($truncate, ' ');
if ($html)
{
$truncateCheck = Tools::substr($truncate, 0, $spacepos);
$lastOpenTag = Tools::strrpos($truncateCheck, '<');
$lastCloseTag = Tools::strrpos($truncateCheck, '>');
if ($lastOpenTag > $lastCloseTag)
{
preg_match_all('/<[\w]+[^>]*>/s', $truncate, $lastTagMatches);
$lastTag = array_pop($lastTagMatches[0]);
$spacepos = Tools::strrpos($truncate, $lastTag) + Tools::strlen($lastTag);
}
$bits = Tools::substr($truncate, $spacepos);
preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
if (!empty($droppedTags))
{
if (!empty($openTags))
{
foreach ($droppedTags as $closingTag)
if (!in_array($closingTag[1], $openTags))
array_unshift($openTags, $closingTag[1]);
}
else
{
foreach ($droppedTags as $closingTag)
$openTags[] = $closingTag[1];
}
}
}
$truncate = Tools::substr($truncate, 0, $spacepos);
}
$truncate .= $ellipsis;
if ($html)
foreach ($openTags as $tag)
$truncate .= '</' . $tag . '>';
return $truncate;
}
public static function normalizeDirectory($directory)
{
$last = $directory[strlen($directory) - 1];
if (in_array($last, array('/', '\\')))
{
$directory[strlen($directory) - 1] = DIRECTORY_SEPARATOR;
return $directory;
}
$directory .= DIRECTORY_SEPARATOR;
return $directory;
} Preg_replace ile ilgili
2
●449
- 21-12-2014, 14:30:46merhaba, örneğin ps 1.5 diye girmiş olduğum bir ürün url olarak siteadi.com/ps-15 olarak ekranda görünüyor bu urlyi siteadi.com/ps-1-5 olarak nasıl yapabilirim
- 21-12-2014, 14:43:56Şu kısmı
// Remove all non-whitelist chars. if ($allow_accented_chars) $str = preg_replace('/[^a-zA-Z0-9\s\'\:\/\[\]-\pL]/u', '', $str); else $str = preg_replace('/[^a-zA-Z0-9\s\'\:\/\[\]-]/','', $str); $str = preg_replace('/[\s\'\:\/\[\]\-]+/', ' ', $str); $str = str_replace(array(' ', '/'), '-', $str);şununla değiştirip dener misin?
// Remove all non-whitelist chars. if ($allow_accented_chars) $str = preg_replace('/[^a-zA-Z0-9\.\s\'\:\/\[\]-\pL]/u', '', $str); else $str = preg_replace('/[^a-zA-Z0-9\.\s\'\:\/\[\]-]/','', $str); $str = preg_replace('/[\s\'\:\/\[\]\-]+/', ' ', $str); $str = str_replace(array(' ', '/', '.'), '-', $str); - 21-12-2014, 14:57:20maalesef bir değişiklik olmadı acaba .htaccess dosyasında da değişiklik yapmak gerekiyor olabilirmisoulmy adlı üyeden alıntı: mesajı görüntüle
<IfModule mod_rewrite.c> <IfModule mod_env.c> SetEnv HTTP_MOD_REWRITE On </IfModule> RewriteEngine on RewriteRule . - [E=REWRITEBASE:/] RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L] # Images RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L] RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L] RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L] RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L] RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L] RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L] RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L] RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L] RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L] RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L] # AlphaImageLoader for IE and fancybox RewriteRule ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L] # Dispatcher RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ %{ENV:REWRITEBASE}index.php [NC,L] </IfModule> AddType application/vnd.ms-fontobject .eot AddType font/ttf .ttf AddType font/otf .otf AddType application/x-font-woff .woff #If rewrite mod isn't enabled ErrorDocument 404 /index.php?controller=404 # ~~end~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again