Is there a php function to convert these characters? - php

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('&#233', '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("'", '’', '–', '“', '"', '&#233', '&');
$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

Related

how to optimize. speed up this php function

there are three PHP functions I need to optimize this code, speedup, please helpme
this all functions I use for PHP classified site,
brandwords - is to give more details if some one enter brands
clean_url - is for clean unwanted characters from URL , all
clean_title - is for clean unwanted characters from Title , all
function brandwords( $in ) {
$wordToFind = array(
'suzuki' => 'Suzuki' ,
'Toyota' => 'Toyota',
-------------------------------- big database.
);
$words = preg_split('/\s+/', remove_numbers(strtolower(trim($in))));
foreach ( $wordToFind as $key => $value ) {
if ( ( $pos = array_search( strtolower( $key ), $words ) ) !== FALSE ) {
return $value;
exit();
}
-----------------------------------------------------------------------
function clean_url( $text ) {
$text = strtolower( trim( $text ) );
$code_entities_match = array(' ','--','"','!','#','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');
$code_entities_replace = array('-','-','','','','','','','','','','','','','','','','','','','','','','','','');
$text = str_replace($code_entities_match, $code_entities_replace, $text);
$text = str_replace('/[^a-zA-Z0-9\-]/', '-', $text);
$text = str_replace('/^[\-]+/', '', $text);
$text = str_replace('/[\-]+$/', '', $text);
$text = str_replace('/[\-]{2,}/', '-', $text);
$code_entities_match1 = array('--','---','----');
$code_entities_replace1 = array('-','-','-');
$text = str_replace($code_entities_match1, $code_entities_replace1, $text);
return $text;
//$text2 = ucfirst(str_replace('-',' ',$text1));
}
----------------------------------------------------------------
function clean_title( $text ) {
$text = strtolower( trim( $text ) );
$code_entities_match = array(' ','--','"','!','#','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');
$code_entities_replace = array('-','-','','','','','','','','','','','','','','','','','','','','','','','','');
$text = str_replace($code_entities_match, $code_entities_replace, $text);
$text = str_replace('/[^a-zA-Z0-9\-]/', '-', $text);
$text = str_replace('/^[\-]+/', '', $text);
$text = str_replace('/[\-]+$/', '', $text);
$text = str_replace('/[\-]{2,}/', '-', $text);
$code_entities_match1 = array('--','---','----');
$code_entities_replace1 = array('-','-','-');
$text = str_replace($code_entities_match1, $code_entities_replace1, $text);
$text = ucfirst(str_replace('-',' ',$text));
return $text;
}
-------------------------------------------------------------
function clean_desc( $text ) {
$text = strtolower( trim( $text ) );
$code_entities_match = array('.','/','\r','\n',' ','--','"','!','#','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",'.','/','*','+','~','`','=');
$code_entities_replace = array(' ',' ',' ',' ','-','-',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','-','-',' ',' ',' ',' ',' ');
$text = str_replace($code_entities_match, $code_entities_replace, $text);
$text = str_replace('/[^a-zA-Z0-9\-]/', '-', $text);
$text = str_replace('/^[\-]+/', '', $text);
$text = str_replace('/[\-]+$/', '', $text);
$text = str_replace('/[\-]{2,}/', '-', $text);
//$text = preg_replace("'\b[a-z0-9]{1,3}\b'i",' ',$text);
$code_entities_match1 = array('--','---','----');
$code_entities_replace1 = array('-','-','-');
$text = str_replace($code_entities_match1, $code_entities_replace1, $text);
$text = ucfirst(str_replace('-',' ',$text));
return $text;
}

How to hide text between multiple <> // [] **

I use this code and all is ok, it hide text from $comments contains between [], but i want to hide text from other symbols for. ex. ** && ^^ $$ ## // <>. What i need to add here, to have INSTEAD OF
Date <20.02.2013> Time [11-00] Name #John#
Have this:
Date Time Name
?
function replaceTags($startPoint, $endPoint, $newText, $source) {
return preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si', '$1'.$newText.'$3', $source);
}
$source= $comments;
$startPoint='[';
$endPoint=']';
$newText='';
echo replaceTags($startPoint, $endPoint, $newText, $source);
You just need to change
$startPoint='[';
$endPoint=']';
to
$startPoint='<';
$endPoint='>';
To do multiple symbols you can do multiple calls to the function, like this:
$source= $comments;
$newText='';
$str = replaceTags('[', ']', $newText, $source);
$str = replaceTags('<', '>', $newText, $str);
$str = replaceTags('*', '*', $newText, $str);
$str = replaceTags('&', '&', $newText, $str);
$str = replaceTags('^', '^', $newText, $str);
$str = replaceTags('$', '$', $newText, $str);
$str = preg_replace("/\#[^#]+#)/","",$str);
$str = replaceTags('/', '/', $newText, $str);
// add more here
echo $str;
You will have to create patterns for each pair:
$pairs = array(
'*' => '*',
'&' => '&',
'^' => '^',
'$' => '$',
'#' => '#',
'/' => '/',
'[' => ']', // this
'<' => '>', // and this pairs differently from the rest
);
$patterns = array();
foreach ($pairs as $start => $end) {
$start = preg_quote($start, '/');
$end = preg_quote($end, '/');
$patterns[] = "/($start)[^$end]*($end)/";
}
echo preg_replace($patterns, '', $s), PHP_EOL;
// not sure what you want here
//  echo preg_replace($patterns, '$1' . $newText . '$2', $s), PHP_EOL;

Passing Dynamic arguments to a function

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;
}

str_replace between two numerical strings

Another problem with str_replace, I would like to change the following $title data into URL by taking the $string between number in the beginning and after dash (-)
Chicago's Public Schools - $10.3M
New Jersey - $3M
Michigan: Public Health - $1M
The desire output is:
chicago-public-school
new-jersey
michigan-public-health
PHP code I am using
$title = ucwords(strtolower(strip_tags(str_replace("1: ", "", $title))));
$x = 1;
while ($x <= 10) {
$title = ucwords(strtolower(strip_tags(str_replace("$x: ", "", $title))));
$x++;
}
$link = preg_replace('/[<>()!#?:.$%\^&=+~`*é"\']/', '', $title);
$money = str_replace(" ", "-", $link);
$link = explode(" - ", $link);
$link = preg_replace(" (\(.*?\))", "", $link[0]);
$amount = preg_replace(" (\(.*?\))", "", $link[1]);
$code_entities_match = array(''s', '"', '!', '#', '#', '$', '%', '^', '&', '*', '(', ')', '+', '{', '}', '|', ':', '"', '<', '>', '?', '[', ']', '', ';', "'", ',', '.', '_', '/', '*', '+', '~', '`', '=', ' ', '---', '--', '--');
$code_entities_replace = array('', '-', '-', '', '', '', '-', '-', '', '', '', '', '', '', '', '-', '', '', '', '', '', '', '', '', '', '-', '', '-', '-', '', '', '', '', '', '-', '-', '-', '-');
$link = str_replace($code_entities_match, $code_entities_replace, $link);
$link = strtolower($link);
Unfortunately the result I got:
-chicagoamp9s-public-school
2-new-jersey
3-michigan-public-health
Anyone has a better solution for this? Thanks guys!
(the ' changed into amp9 - wonder why?)
If I understand you correctly:
if (preg_match('!\d+:\s+(.*)\s+-\s+\$\d+(?:\.\d+)?!', $title, $groups)) {
$words = strip_tags(strtolower($groups[1]));
$words = preg_replace('\[^\s\w]!', '', $words);
$words = preg_replace('!\s+!', '-', $words);
$words = preg_replace('!-+!', '-', $words);
echo $words;
}
One thing: your text has "1. Chicago's..." not "1: ..." like your code would seem to suggest. Is one an error or is there something else going on?
You can do:
$str = "1. Chicago's Public Schools - $10.3M";
$from = array('/^\d+\.\s+([^-]*) -.*$/','/[^A-Z ]/i','/\s+/');
$to = array("$1",'','-');
$str = strtolower(preg_replace($from,$to,$str));
echo $str; // prints chicagos-public-schools
<?php
$lines = array("1. Chicago's Public Schools - $10.3M",
"2. New Jersey - $3M",
"3. Michigan: Public Health - $1M"
);
// remove the number bullets
$lines = preg_replace('/\ - \$\d*\.?\d*M$/', '', $lines);
// remove the trailing dollar amount
$lines = preg_replace('/^\d+\.\ /', '', $lines);
// remove ignore chars
$ignore_pattern = "/['s|:]/";
$lines = preg_replace($ignore_pattern, '', $lines);
for ($i=0; $i<count($lines); $i++) {
$lines[$i] = implode('-',explode(' ', strtolower(trim($lines[$i]))));
}
print_r($lines);
and the output:
Array
(
[0] => chicago-public-school
[1] => new-jerey
[2] => michigan-public-health
)
EDIT Start:
<?php
$lines = array("1. Chicago's Public Schools - $10.3M",
"2. New Jersey - $3M",
"3. Michigan: Public Health - $1M",
"4. New York's Starbucks - $2M",
);
$lines = preg_replace('/\ - \$\d*\.?\d*M$/', '', $lines);
$lines = preg_replace('/^\d+\.\ /', '', $lines);
$ignore_strings = array("'s", ':');
for ($i=0; $i<count($lines); $i++) {
foreach ($ignore_strings as $s) {
$lines[$i] = str_replace($ignore_strings, '', $lines[$i]);
}
}
for ($i=0; $i<count($lines); $i++) {
$lines[$i] = implode('-',explode(' ', strtolower(trim($lines[$i]))));
}
print_r($lines);
output:
Array
(
[0] => chicago-public-schools
[1] => new-jersey
[2] => michigan-public-health
[3] => new-york-starbucks
)
Hope it meets your needs.
EDIT End.
Assuming you already have extracted titles correctly like "Chicago's Public Schools", then to generate pagenames out of them:
function generatePagename($s) {
//to lower
$pagename = trim(html_entity_decode(strtolower($s), ENT_QUOTES));
//remove 's
$pagename = trim(preg_replace("/(\'s)/", "", $pagename));
//replace special chars with spaces
$pagename = trim(preg_replace("/[^a-z0-9\s]/", " ", $pagename));
//replace spaces with dashes
$pagename = trim(preg_replace("/\s+/", "-", $pagename));
return $pagename;
}
Which will convert something like
Chicago's "Public": Scho-ols1+23
to
chicago-public-scho-ols1-23.
Finally I looked back to initial code and have some fixes:
$title = ucwords(strtolower(strip_tags(str_replace("1. ","",$title))));
$x=1;
while($x <= 10) {
$title = ucwords(strtolower(strip_tags(str_replace("$x. ","",$title))));
$x++;
}
$data = preg_replace('/[<>()!#?:.$%\^&=+~`*&#;"\']/', '',$title);
$urldata = str_replace(" ","-",$data);
$data = explode(" - ",$data);
$link = preg_replace(" (\(.*?\))", "", $data[0]);
$budget = preg_replace(" (\(.*?\))", "", $data[1]);
$code_entities_match = array( '"' ,'!' ,'#' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'+' ,'{' ,'}' ,'|' ,':' ,'"' ,'<' ,'>' ,'?' ,'[' ,']' ,'' ,';' ,"'" ,',' ,'.' ,'_' ,'/' ,'*' ,'+' ,'~' ,'`' ,'=' ,' ' ,'---' ,'--','--');
$code_entities_replace = array('' ,'-' ,'-' ,'' ,'' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-' ,'' ,'-' ,'-' ,'' ,'' ,'' ,'' ,'' ,'-' ,'-' ,'-','-');
$link = str_replace($code_entities_match, $code_entities_replace, $link);
$link = strip_tags(str_replace("amp39s","",$link));
$link = strtolower($link);
What a mess, I know, but it works anyway, thanks guys for helping me, especially cletus who found the mistake between 1. and 1:

How to remove all special characters from URL?

I have my class
public function convert( $title )
{
$nameout = strtolower( $title );
$nameout = str_replace(' ', '-', $nameout );
$nameout = str_replace('.', '', $nameout);
$nameout = str_replace('æ', 'ae', $nameout);
$nameout = str_replace('ø', 'oe', $nameout);
$nameout = str_replace('å', 'aa', $nameout);
$nameout = str_replace('(', '', $nameout);
$nameout = str_replace(')', '', $nameout);
$nameout = preg_replace("[^a-z0-9-]", "", $nameout);
return $nameout;
}
BUt I can't get it to work when I use special characters like ö and ü and other, can sombody help me here? I use PHP 5.3.
The first answer in this SO thread contains the code you need to do this.
And what about:
<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
From: http://php.net/manual/en/function.urlencode.php
I wrote this function a while ago for a project I was working on and couldn't get RegEx to work. Its not the best way, but it works.
function safeURL($input){
$input = strtolower($input);
for($i = 0; $i < strlen($input); $i++){
$working = ord(substr($input,$i,1));
if(($working>=97)&&($working<=122)){
//a-z
$out = $out . chr($working);
} elseif(($working>=48)&&($working<=57)){
//0-9
$out = $out . chr($working);
} elseif($working==46){
//.
$out = $out . chr($working);
} elseif($working==45){
//-
$out = $out . chr($working);
}
}
return $out;
}
Here's a function to help with what you're doing, it's written in
Czech: http://php.vrana.cz/vytvoreni-pratelskeho-url.php
(and translated to English)
Here's another take on it (from the Symfony documentation):
<?php
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}

Categories