I have a user defined function below:
function char_replace($line1){
$line1= str_ireplace("Snippet:", "", $line1);
// First, replace UTF-8 characters.
$line1= str_replace(
array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
array("'", "'", '"', '"', '-', '--', '...'),
$line1);
// Next, replace their Windows-1252 equivalents.
$line1= str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array("'", "'", '"', '"', '-', '--', '...'),
$line1);
}
and i'm replacing characters on multiple lines that i've exploded, except i want to apply a dynamic argument to the function char_replace where $line could very well be $line2 or $line3 so i would convert the characters this way:
$line1 = char_replace($line1)
I want to make the function arguments and the str_replace/str_ireplace arguments to be a dynamic variable, where i could just convert another line like so:
$random_line = char_replace($random_line)
Is this possible?
If I'm reading this right, just add a return to the function. So:
function char_replace($string){
$string= str_ireplace("Snippet:", "", $string);
// First, replace UTF-8 characters.
$string= str_replace(
array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
array("'", "'", '"', '"', '-', '--', '...'),
$string);
// Next, replace their Windows-1252 equivalents.
$string= str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array("'", "'", '"', '"', '-', '--', '...'),
$string);
return $string;
}
This will allow you to pass any string to the function and get the modified string back.
Assuming that you end your function with return $line1; you can call it like this:
$line1 = char_replace($line1);
$line2 = char_replace($line2);
$line3 = char_replace($line3);
How you call the arguments in your function definition doesn't matter, they're local to that function and can have a different name outside of it.
Do you just want to add return statement to your function:
function char_replace($line1){
$line1= str_ireplace("Snippet:", "", $line1);
// First, replace UTF-8 characters.
$line1= str_replace(
array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
array("'", "'", '"', '"', '-', '--', '...'),
$line1);
// Next, replace their Windows-1252 equivalents.
$line1= str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array("'", "'", '"', '"', '-', '--', '...'),
$line1);
return $line1;
}
Related
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 need to find if a file name contains some special characters I don't want.
I'm using this code actually:
$files = array("logo.png", "légo.png");
$badChars = array(" ", "é", "É", "è", "È", "à", "À", "ç", "Ç", "¨", "^", "=", "/", "*", "-", "+", "'", "<", ">", ":", ";", ",", "`", "~", "/", "", "|", "!", "#", "#", "$", "%", "?", "&", "(", ")", "¬", "{", "}", "[", "]", "ù", "Ù", '"', "«", "»");
$matches = array();
foreach($files as $file) {
$matchFound = preg_match_all("#\b(" . implode("|", $badChars) . ")\b#i", $file, $matches);
}
if ($matchFound) {
$words = array_unique($matches[0]);
foreach($words as $word) {
$results[] = array('Error' => "Forbided chars found : ". $word);
}
}
else {
$results[] = array('Success' => "OK.");
}
But I have an error saying:
Warning: preg_match_all(): Compilation failed: nothing to repeat at offset 38 in /home/public_html/upload.php on line 138
Which is:
$matchFound = preg_match_all("#\b(" . implode("|", $badChars) . ")\b#i", $file, $matches);
Any help or clue?
it is because ? * + are quantifiers. Since they are not escaped you obtain this error: |? there is obviously nothing to repeat.
For your task you don't need to use an alternation, a character class should suffice:
if (preg_match_all('~[] éèàç¨^=/*-+\'<>:;,`\~/|!##$%?&()¬{}[ù"«»]~ui', $file, $m)) {
$m = array_unique($m[0]);
$m = array_map(function ($i) use ($file) { return array('Error' => 'Forbidden character found : ' . $i . ' in ' . $file); }, $m);
$results = array_merge($results, $m);
}
or perhaps this pattern: ~[^[:alnum:]]~
It's because your characters have * in it, which tries to repeat the previous character, which in your case ends up being |, which is invalid. Your regex turns into:
..... |/|*|-| .....
Map preg_quote() to your character array before your loop and you'll be fine:
$badChars = array_map( 'preg_quote', $badChars);
Just make sure that since you're not specifying your delimiter # in the call to preg_quote(), you'll have to manually escape it in your $badChars array.
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 get data from the database that is utf8 encoded. But somehow some old data contains latin1 characters.
So this
$encod = mb_detect_encoding($string, 'UTF-8', true);
always is correct.
Is it safe to always use utf8_decode() to check for latin1 characters like 'äöüß'???
$string = utf8_decode($string);
$search = Array(" ", "ä", "ö", "ü", "ß", "."); //,"/Ä/","/Ö/","/Ü/");
$replace = Array("-", "ae", "oe", "ue", "ss", "-"); //,"Ae","Oe","Ue");
$string = str_replace($search, $replace, strtolower($string));
Regards
It seems to work without the utf8_encoding:
<?php
$string = "äöüß";
$search = Array(" ", "ä", "ö", "ü", "ß", "."); //,"/Ä/","/Ö/","/Ü/");
$replace = Array("-", "ae", "oe", "ue", "ss", "-"); //,"Ae","Oe","Ue");
$string = str_replace($search, $replace, strtolower($string));
echo $string;
?>
DEMO: http://codepad.org/HGTyHkBU
Use htmlspecialchars(); it is more safer for work.
More info:
http://php.net/manual/en/function.htmlspecialchars.php