WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-15-2008, 09:40 PM
WebProWorld Member
 

Join Date: Jan 2006
Location: USA
Posts: 38
krnl RepRank 0
Default Strings and arrays in PHP

I have output from a database being stored as a string variable and then used as the meta keywords on my page. So, let's say...

$keywords = "welcome,to,my,website";

Now, I want to remove the two character words (to and my) from this string so that I am left with:

$keywords = "welcome,website";

I tried to explode the $keywords variable, remove the items with a strlen less than 3 and then implode the array, but it's not working. Here is my code.

Code:
$aKeywords = explode(",", $keywords);
$final_keywords = array();

foreach ($aKeywords as $keyword) {
      if (strlen($keyword) < 3) {
         $keyword = '';
      }
      $final_keywords[$i] = $keyword;
}

$keywords = implode(",",$final_keywords);
What am I doing wrong? Is there a better approach or am I completely off-base?

Thanks in advance.
__________________
Rick - Linux Tutorials for Beginners
allin-enterprises.com | all-in-general.com | MagliteSales.com | DiscountFishTackle.com
Reply With Quote
  #2 (permalink)  
Old 03-16-2008, 09:11 AM
DaveSawers's Avatar
WebProWorld Veteran
 

Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 354
DaveSawers RepRank 1
Default Re: Strings and arrays in PHP

I would change it around and simplify it a bit...

Code:
$aKeywords = explode(",", $keywords);

for ($i=0; $i<count($aKeywords); $i++) {
      if (strlen($keyword) > 2) {
            $final_keywords[] = $keyword;
      }
}

$keywords = implode(",",$final_keywords);
I haven't tried your code (or mine) but in what way doesn't yours work?
__________________
Dynamic Software Development
www.activeminds.ca
Reply With Quote
  #3 (permalink)  
Old 03-16-2008, 11:48 AM
WebProWorld Member
 

Join Date: Jan 2006
Location: USA
Posts: 38
krnl RepRank 0
Default Re: Strings and arrays in PHP

I implemented a couple of your changes and got it to work.

1) Switched the if to include keywords of 3 chars or more rather than set keywords of less than 3 to "". (that was kinda silly of me anyway!)

2) In my final_keywords array, I had $final_keywords[$i] = $keyword . I changed that by removing the $i.

Here's the resulting code:
Code:
$aKeywords = explode(",", $keywords);

foreach ($aKeywords as $keyword) {
  if (strlen($keyword) > 2) {
      $final_keywords[] = $keyword;
  }
}

$keywords = implode(",",$final_keywords);
Thanks for your assistance!
__________________
Rick - Linux Tutorials for Beginners
allin-enterprises.com | all-in-general.com | MagliteSales.com | DiscountFishTackle.com
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Blocking Bots from Query Strings wige Search Engine Optimization Forum 9 11-24-2007 02:24 PM
Duplicate content and tracking strings buddhu Google Discussion Forum 1 07-14-2006 03:03 PM
Microsoft buy comes with strings attached WPW_Feedbot IT Discussion Forum 0 12-17-2004 05:30 PM
Adding to Arrays ackerley1 Web Programming Discussion Forum 2 09-14-2004 09:36 AM
Php strings PR bleed timy Search Engine Optimization Forum 3 04-06-2004 07:00 AM


Search Engine Optimization by vBSEO 3.2.0