Cleaning string problem
8 posts by 2 authors in: Forums > CMS Builder
Last Post: February 18, 2009 (RSS)
I have an editor call "Tags" with only one field call "tag"
I have no problem to display my list of "tag" using the code bellow:
<?php foreach ($tags_musiqueRecords as $record): ?>
<?php echo $record['tag'] ?><br/>
<br/>
<?php endforeach; ?>
What i want to do is to use the function bellow to clean my string (tag), for example remove accent, replace spaces by "-"...
See the function bellow
function rewrite($label)
{
$search = array ('@[éèêëÊË]@i','@[àâäÂÄ]@i','@[îïÎÏ]@i','@[ûùüÛÜ]@i','@[ôöÔÖ]@i',
'@[ç]@i','@[^a-zA-Z0-9]@');
$replace = array ('e','a','i','u','o','c',' ');
$label = preg_replace($search, $replace, $label);
$label = strtolower($label); // lowercase
$label = str_replace(" ",'-',$label); //
$label = preg_replace('#\-+#','-',$label); //
$label = preg_replace('#([-]+)#','-',$label);
trim($label,'-'); //
return $label
}
Here is what i have tryed and it doesn't work
<?php foreach ($tags_musiqueRecords as $record): ?>
<?php
$label=$record['tag'];
function rewrite($label)
{
$search = array ('@[éèêëÊË]@i','@[àâäÂÄ]@i','@[îïÎÏ]@i','@[ûùüÛÜ]@i','@[ôöÔÖ]@i',
'@[ç]@i','@[^a-zA-Z0-9]@');
$replace = array ('e','a','i','u','o','c',' ');
$label = preg_replace($search, $replace, $label);
$label = strtolower($label);
$label = str_replace(" ",'-',$label);
$label = preg_replace('#\-+#','-',$label);
$label = preg_replace('#([-]+)#','-',$label);
trim($label,'-');
return $label;
}
?>
<br/>
<?php endforeach; ?>
Here is the returned error:
Fatal error: Cannot redeclare rewrite() (previously declared in /var/www/vhosts/......./test.php:25) in /var/www/vhosts/........./test.php on line 25
[pirate]
Re: [willbegood] Cleaning string problem
By Dave - February 18, 2009
>Fatal error: Cannot redeclare rewrite() ...
You can only define a function once. That error means you have the same function twice and one copy at line 25 in test.php.
Remove a copy of "function rewrite() ... " so you only have that once. You can call the rewrite() function as many times as you want, but once declare it once in your PHP files. Otherwise it won't know which one to use.
Hope that helps!
interactivetools.com
Re: [Dave] Cleaning string problem
Re: [willbegood] Cleaning string problem
By Dave - February 18, 2009
Double check any other files that are include()'d or require()'d from your file and that they don't have a copy of that function. That error definitely means that PHP has seen two copies of a function with the same name.
Hope that helps!
interactivetools.com
Re: [Dave] Cleaning string problem
<?php
require_once "/var/www/vhosts/onsemobilisepourvous.com/httpdocs/client/hello_gp/admin/lib/viewer_functions.php";
list($tags_musiqueRecords, $tags_musiqueMetaData) = getRecords(array(
'tableName' => 'tags_musique',
));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style type="text/css">
body { font-family: arial; }
.instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
</style>
</head>
<body>
<?php foreach ($tags_musiqueRecords as $record): ?>
<?php
$label=$record['tag'];
function rewrite($label)
{
$search = array ('@[éèêëÊË]@i','@[àâäÂÄ]@i','@[îïÎÏ]@i','@[ûùüÛÜ]@i','@[ôöÔÖ]@i',
'@[ç]@i','@[^a-zA-Z0-9]@');
$replace = array ('e','a','i','u','o','c',' ');
$label = preg_replace($search, $replace, $label);
$label = strtolower($label);
$label = str_replace(" ",'-',$label);
$label = preg_replace('#\-+#','-',$label);
$label = preg_replace('#([-]+)#','-',$label);
trim($label,'-');
return $label;
}
?>
<?php echo $record['tag'] ?><br/>
<?php endforeach; ?>
<?php if (!$tags_musiqueRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP2: Display Records -->
</body>
</html>
Re: [willbegood] Cleaning string problem
By Dave - February 18, 2009
Let me know if that fixes it for you.
interactivetools.com
Re: [Dave] Cleaning string problem
Somothing i did not tell you, it display the last record then an error
see bellow:
Electroniqueééééèèè (this is my last record)
Fatal error: Cannot redeclare rewrite2() (previously declared in /var/...test2.php:25) in /var/.../test2.php on line 25
Re: [willbegood] Cleaning string problem
By Dave - February 18, 2009
<?php
function rewrite($label)
{
...
}
?>
interactivetools.com