Page 1 of 1

Trim Exploded Array: works, suggestions for improvements?

Posted: Wed Nov 05, 2008 7:25 pm
by JAB Creations
I decided I need to trim tags before they are inserted in to a database for my blog. I create this little number though I'm curious as to how others would approach it and what possible improvements could be made. The script does work so I'm simply interested in how others would approach this. :mrgreen:

Code: Select all

<?php
$my_string1  = "piece1 , piece2 , piece3 , piece4 , piece5 , piece6";
 
$pieces = explode(",", $my_string1);
 
foreach($pieces AS $piece1)
{
 $piece2 = trim($piece1);
 echo "<div>$piece2</div>\n";
}
?>
XHTML Output

Code: Select all

<div>piece1</div><div>piece2</div><div>piece3</div><div>piece4</div><div>piece5</div><div>piece6</div>

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Wed Nov 05, 2008 9:17 pm
by Syntac
This would remove the need for trimming.

Code: Select all

$pieces = explode(" , ", $my_string1); // Note the spaces

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Wed Nov 05, 2008 9:25 pm
by requinix
Hmm...

Code: Select all

$my_string1 = "piece1 , piece2,     piece 3, piece 4    ,    piece 5";
preg_match_all('/(^|,)\s*([^ ,]+(\s+[^ ,]+)*)\s*(?=,|$)/', $my_string1, $matches);
$pieces = $matches[2];

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Wed Nov 05, 2008 10:05 pm
by infolock
Syntac wrote:This would remove the need for trimming.

Code: Select all

$pieces = explode(" , ", $my_string1); // Note the spaces
above works good. if you want more trim, try :

Code: Select all

 
$foo = explode(" , ", $my_string);
$bar = array_map("trim", $foo);
unset($foo);
 

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Thu Nov 06, 2008 8:02 am
by JAB Creations
All good suggestions and I didn't think about what Syntac suggested. However if I did I would imagine the point tasairis brought up would negate getting around the use of the trim() function.

This seems to be the current minimal working example...

Code: Select all

$my_string1  = " piece1 ,  piece2 ,  piece3 ,  piece4 ,  piece5 ,  piece6 ";
 
$pieces = explode(",", $my_string1);
$bar = array_map("trim", $pieces);
//unset($pieces);
 
foreach($pieces as $piece1)
{
 $piece2 = trim($piece1);
 echo "<div>$piece2</div>\n";
}
Is there a necessary reason to unset $pieces?

Thank you infolock, I am now aware of the array_map function. :)

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Thu Nov 06, 2008 8:18 am
by VladSun
JAB Creations wrote:Is there a necessary reason to unset $pieces?
You don't need it any more, so you should free the memory it's using.

PS: You code is wrong - you should use

Code: Select all

foreach($bar as $piece)
and you don't need the trim() call any more.

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Thu Nov 06, 2008 8:44 am
by JAB Creations
Thanks VladSun, I didn't look closely at the second half...ok so in reiteration of my prior post here is the minimal working example...

Code: Select all

<?php
$my_string1  = " piece1 ,  piece2 ,  piece3 ,  piece4 ,  piece5 ,  piece6 ";
 
$pizza = explode(",", $my_string1);
$pizza_trimmed = array_map("trim", $pizza);
unset($pizza);
 
foreach($pizza_trimmed as $piece)
{
 echo "<div>$piece</div>\n";
}
?>

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Thu Nov 06, 2008 9:32 am
by VladSun
Well, in this particular case I would go with Syntac's suggestion. It's more elegant and memory friendly.

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Thu Nov 06, 2008 11:04 am
by JAB Creations
VladSun wrote:Well, in this particular case I would go with Syntac's suggestion. It's more elegant and memory friendly.
True though only to a certain extent, consider the possibility of two or more extra spaces.

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Thu Nov 06, 2008 11:17 am
by VladSun
Where does the value of $my_string1 come from?

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Thu Nov 06, 2008 11:26 am
by JAB Creations
In the real world application it will be a single text input with tags separated by commas.

Re: Trim Exploded Array: works, suggestions for improvements?

Posted: Thu Nov 06, 2008 11:36 am
by JAB Creations
Current example code...

Code: Select all

$thread_tags = mysql_real_escape_string($_POST['post_tags']);
 $thread_count = database_check("threads", "thread_title", $thread_title);
 
 
 if ($thread_count == "0")
 {
 /*** blog thread handled here***/
  $piece = explode(",", $thread_tags);
  $pieces = array_map("trim", $piece);
  unset($piece);
 
  foreach($pieces as $piece)
  {
   $piece_base = name_base($piece);
   $mysql_result = mysql_query("INSERT INTO tags (tag_name, tag_name_base) VALUES ('".$piece."', '".$piece_base."')");
   if (!$mysql_result) {$_SESSION['status'] = '<b>Error occurred adding tag to database:</b> '. mysql_error(); header("location:".$truepage."?error"); exit();}
  }
 }
else
 {
  $_SESSION['status'] = '<b>Error adding blog:</b> a blog with the same title already exists!';
 }
}