Cleaning string problem

8 posts by 2 authors in: Forums > CMS Builder
Last Post: February 18, 2009   (RSS)

Hello i'm building “Filterable” Portfolio with jQuery, and i need to play with cleaning string stuff

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: [Dave] Cleaning string problem

dave here is my entire file code, i don't have same fonction twice...


Re: [willbegood] Cleaning string problem

By Dave - February 18, 2009

That code didn't get posted? You can try attaching it as a file if needed.

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!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Cleaning string problem

sorry dave... see bellow


<?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

If you're still getting that error try renaming the function rewrite2.

Let me know if that fixes it for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Cleaning string problem

no same 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

Oh, I see. It's because you are declaring the function inside a foreach loop. So each "loop" it's declared again. Try putting it at the top of your script:

<?php
function rewrite($label)
{
...
}
?>
Dave Edis - Senior Developer
interactivetools.com