I'm trying to create some clean URLs for artist names. I'm using the following to parse Pearl Jam to pearl-jam. All is fine until it tries to process KoЯn. Instead of returning korn, it is returning koand1071n. In my database, KoЯn is actually stored as
KoЯn
I'm using this code below. What can I do to handle the backwards R?
$delimiter = "-";
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = str_replace("\$", "s", $clean);
$clean = preg_replace( '/&/', 'and', $clean );
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
$clean = strtolower(trim($clean, '-'));
Just replace Я with a r with str_replace like you do for $
$clean = str_replace("Я", "r", $clean);
or
$clean = str_replace("Я", "r", $clean);
Related
This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Deprecated: Function eregi_replace() [duplicate]
(2 answers)
Closed 4 years ago.
How can I adjust my code by replacing the "eregi_replace" function with another one that does the same thing?
I know the alternatives, but I do not know how to update the code. O erro que recebo é:
PHP Deprecated: Function eregi_replace
function url($String){
$Separador = "-";
$String = trim($String);
$String = strtolower($String);
$String = strip_tags($String);
$String = eregi_replace("[[:space:]]", $Separador, $String);
$String = eregi_replace("[çÇ]", "c", $String);
$String = eregi_replace("[áÁäÄàÀãÃâÂ]", "a", $String);
$String = eregi_replace("[éÉëËèÈêÊ]", "e", $String);
$String = eregi_replace("[íÍïÏìÌîÎ]", "i", $String);
$String = eregi_replace("[óÓöÖòÒõÕôÔ]", "o", $String);
$String = eregi_replace("[úÚüÜùÙûÛ]", "u", $String);
$String = eregi_replace("(\()|(\))", $Separador, $String);
$String = eregi_replace("(\/)|(\\\)", $Separador, $String);
$String = eregi_replace("(\[)|(\])", $Separador, $String);
$String = eregi_replace("[#®#\$%&\*\+=\|º]", $Separador, $String);
$String = eregi_replace("[;:'\"<>,\.?!_]", $Separador, $String);
$String = eregi_replace("[“”]", $Separador, $String);
$String = eregi_replace("(ª)+", $Separador, $String);
$String = eregi_replace("[´~^°]", $Separador, $String);
$String = eregi_replace("($Separador)+", $Separador, $String);
$String = substr($String, 0, 100);
$String = eregi_replace("(^($Separador)+)|(($Separador)+$)", "", $String);
$String = str_replace("-", $Separador, $String);
return $String;
}
How can I replace "/" and " " from the given string "one size x/l" and generate output like "one-size-x-l" using preg_replace.
Using Regular expression you can replace all the special characters by "-" as below:
$str= "one size x/l";
$str= preg_replace("![^a-z0-9]+!i", "-", $str);
Hope this helps:)
Use the below function, which will make fnie urls as required
function seoUrlAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) { $str = str_replace((array)$replace, ' ', $str); }
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
$a="one size x/l";
echo preg_replace('(/|\s)','-', $a);
got my output as one-size-x-l
I try to generate a slug from a string, but I got some problems with the german umlauts:
$text = 'Ein schöner Text';
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
The result should be: 'ein-schoener-text'
Change the second preg_replace line to the below because to match any letter in any language you need to use \p{L} pattern.
$text = preg_replace('~[^\p{L}\d]+~u', '-', $text);
Code:
<?php
$text = 'Ein schöner Text';
$text = preg_replace('~[^\p{L}\d]+~u', '-', $text);
$text = trim($text, '-');
$text = iconv('utf-8', 'ASCII//TRANSLIT', $text);
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
echo $text;
?>
Output:
ein-schoner-text
I am using a lot of str_replace such as:
$prune = str_replace("'", '\'', $prune);
$prune = str_replace('’', '\'', $prune);
$prune = str_replace('–', '-', $prune);
$prune = str_replace('“', '', $prune);
$prune = str_replace('"', '', $prune);
$prune = str_replace('é', 'e', $prune);
$prune = str_replace('&', '&', $prune);
Is there a simpler PHP function for this, such as htmlspecialchars_decode()?
You are looking for html_entity_decode; it mirrors htmlentities as htmlspecialchars_decode mirrors htmlspecialchars.
You could use arrays in str_replace
$search = array("'", '’', '–', '“', '"', 'é', '&');
$replace = array('\'', '\'', '-', '', '', 'e', '&');
$prune = str_replace($search, $replace, $prune);
Try $prune = htmlspecialchars_decode($prune);
Click http://us1.php.net/manual/en/function.htmlspecialchars-decode.php
I want to reformat the_title(); the exact same way as Wordpress does with the permalinks.
"Borislav Pekić" becomes "borislav-pekic", "Alberto Méndez" becomes "alberto-mendez" and so on.
<?php
$forfattare = the_title();
?>
I've tried to find the way Wordpress does it, but i can't find it. Thanks in advance.
I use this:
<?php function createAlias($name)
{
setlocale(LC_ALL, 'en_US.UTF8');
$name = iconv('UTF-8', 'ASCII//TRANSLIT', $name);
$alias = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $name);
$alias = strtolower(trim($alias, '-'));
$alias = preg_replace("/[\/_|+ -]+/", "-", $alias);
while (substr($alias, -1, 1) == "-") {
$alias = substr($alias, 0, -1);
}
while (substr($alias, 0, 1) == "-") {
$alias = substr($alias, 1, 100);
}
return $alias;
}