Trim Exploded Array: works, suggestions for improvements?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Trim Exploded Array: works, suggestions for improvements?

Post 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>
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

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

Post by Syntac »

This would remove the need for trimming.

Code: Select all

$pieces = explode(" , ", $my_string1); // Note the spaces
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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];
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

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

Post 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);
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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. :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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";
}
?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

Well, in this particular case I would go with Syntac's suggestion. It's more elegant and memory friendly.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

Where does the value of $my_string1 come from?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post by JAB Creations »

In the real world application it will be a single text input with tags separated by commas.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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!';
 }
}
 
Post Reply