Irish SEO,  Marketing & Webmaster Discussion

 

String to array?

This is a discussion on String to array? within the Coding Help forums, part of the Webmaster Help category; I'm currently taking a string from the DB like this ;1|;3|;7| and im changing it to 1 3 7, but ...


Go Back   Irish SEO, Marketing & Webmaster Discussion > Webmaster Help > Coding Help

Register Forum Rules FAQDonate Members List Calendar Search Today's Posts Mark Forums Read


Notices

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 17-01-2008, 11:40 AM
ziycon's Avatar
Wannabe Geek
 
Join Date: Jan 2007
Location: Dublin
Posts: 410
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
ziycon will become famous soon enough
Send a message via MSN to ziycon Send a message via Skype™ to ziycon
Default String to array?

I'm currently taking a string from the DB like this ;1|;3|;7| and im changing it to 1 3 7, but the way i'm doing it wont allow for my to use any number above 9 as they are double digits!
Is there any way to take a string like 1 4 7 10 and put it into an array and remove the whitespace so in array element 0 i have 1 in array element 1 id have 4 etc?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 17-01-2008, 11:44 AM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,048
Nominated 5 Times in 3 Posts
Nominated TOTW/F/M Award(s): 1
louie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enough
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

if you use split function it should work
PHP Code:
$str preg_replace("#[^0-9|]+#","",$str);
$arr_str split('|'$str); 
if you show us the code you have we might be able to help better.
__________________
:. Web Design & Development Web Design Ireland
:. Search Engines Optimization Search Engines Optimization
:. Directory Submission Directory Submission
:. News & Press Release Ireland GiveItSocks.com
:. Used Cars Ireland, Car Parts & Car Audio Cars For Sale, Car Parts & Accessories
:. I Have 2 Find It Directory SEF Directory
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 17-01-2008, 11:55 AM
ziycon's Avatar
Wannabe Geek
 
Join Date: Jan 2007
Location: Dublin
Posts: 410
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
ziycon will become famous soon enough
Send a message via MSN to ziycon Send a message via Skype™ to ziycon
Default

Currently i'm using this function call:
PHP Code:
display_proper_format(display_platform_format(";1|;4|;7|;10|")); 
and these are the methods:
PHP Code:
function display_platform_format($format)
{
    
$format str_replace(";","",$format);
    
$format str_replace("|"," ",$format);
    return 
$format;
}
function 
display_proper_format($formats)
{
    
$size strlen($formats);
    
$number $size 2;
    
$count 0;
    
$i 1;
    while(
$number $count)
    {
        
$item $i;
        if(
$item == 1)
        {
$format "Windows";}
        else if(
$item == 2)
        {
$format .= " Unix";}
        ...
        
$count++;
        
$i += 2;
    }
    
    return 
$format;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 17-01-2008, 12:16 PM
louie's Avatar
Senior Member
 
Join Date: Jan 2006
Location: Dublin, Ireland
Posts: 2,048
Nominated 5 Times in 3 Posts
Nominated TOTW/F/M Award(s): 1
louie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enoughlouie will become famous soon enough
Send a message via Yahoo to louie Send a message via Skype™ to louie
Default

did you try the solution I gave you above?

you should leave the "|" so you can use it as a pointer for the split function.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5 (permalink)  
Old 17-01-2008, 12:25 PM
Hardcore Geek
 
Join Date: Nov 2006
Location: Navan, Meath
Posts: 566
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davidbehan is a name known to alldavidbehan is a name known to alldavidbehan is a name known to alldavidbehan is a name known to alldavidbehan is a name known to alldavidbehan is a name known to all
Send a message via Skype™ to davidbehan
Default

A split function would do this perfect. Haven't done it in php but I do in asp all the time and it's exactly what you want.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6 (permalink)  
Old 17-01-2008, 01:40 PM
ziycon's Avatar
Wannabe Geek
 
Join Date: Jan 2007
Location: Dublin
Posts: 410
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
ziycon will become famous soon enough
Send a message via MSN to ziycon Send a message via Skype™ to ziycon
Default

Quote:
Originally Posted by louie View Post
did you try the solution I gave you above?

you should leave the "|" so you can use it as a pointer for the split function.
Can't try it till later, i'll get back to you when i have!
Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7 (permalink)  
Old 17-01-2008, 03:29 PM
Forbairt's Avatar
respect my AW-THOR-IT-AYY
Recent Blog: Geansai Gorm
 
Join Date: Jun 2007
Location: My Office, Dublin
Posts: 2,101
Nominated 2 Times in 1 Post
Nominated TOTW/F/M Award(s): 1
Forbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enough
Send a message via AIM to Forbairt Send a message via MSN to Forbairt Send a message via Yahoo to Forbairt Send a message via Skype™ to Forbairt
Default

As david said ...


$data = ";1|;3|;7| ";
$array = split('[;|]', $data);
__________________
Forbairt Media | Web Design & Development Galway / Dublin, Ireland - coming soon ... ( vague but descriptive isn't it )
Recent Work: Safari Club African Safari Holidays - Malawi Safaris
Other Stuff: FluffyLinkulator Rapid Inclusion Service Tools
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8 (permalink)  
Old 27-01-2008, 05:25 PM
ziycon's Avatar
Wannabe Geek
 
Join Date: Jan 2007
Location: Dublin
Posts: 410
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
ziycon will become famous soon enough
Send a message via MSN to ziycon Send a message via Skype™ to ziycon
Default

Can't get this to work, any help me???
PHP Code:
function display_proper_format($formats)
{
    
$formats ";1|;3|;7| ";
    
$array split('[;|]'$formats);
    
$i 0;
    
$count sizeof($array);
    while(
$i $count)
    {
        
$item $array[$i];
        if(
$item == 1)
        {
$format "PC";}
        else if(
$item == 2)
        {
$format .= " Xbox";}
        else if(
$item == 3)
        {
$format .= " Xbox360";}
        else if(
$item == 4)
        {
$format .= " PS2";}
        else if(
$item == 5)
        {
$format .= " PS3";}
        else if(
$item == 6)
        {
$format .= " PSP";}
        else if(
$item == 7)
        {
$format .= " GC";}
        else if(
$item == 8)
        {
$format .= " Wii";}
        else if(
$item == 9)
        {
$format .= " DS";}
        else if(
$item == 10)
        {
$format .= " GBA";}
        
$i++;
    }
    
    return 
$format;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9 (permalink)  
Old 27-01-2008, 05:38 PM
Forbairt's Avatar
respect my AW-THOR-IT-AYY
Recent Blog: Geansai Gorm
 
Join Date: Jun 2007
Location: My Office, Dublin
Posts: 2,101
Nominated 2 Times in 1 Post
Nominated TOTW/F/M Award(s): 1
Forbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enough
Send a message via AIM to Forbairt Send a message via MSN to Forbairt Send a message via Yahoo to Forbairt Send a message via Skype™ to Forbairt
Default

works semi fine for me ...

though you are passing in $format to it .. and then defining format in the first line ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10 (permalink)  
Old 27-01-2008, 07:02 PM
Forbairt's Avatar
respect my AW-THOR-IT-AYY
Recent Blog: Geansai Gorm
 
Join Date: Jun 2007
Location: My Office, Dublin
Posts: 2,101
Nominated 2 Times in 1 Post
Nominated TOTW/F/M Award(s): 1
Forbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enoughForbairt will become famous soon enough
Send a message via AIM to Forbairt Send a message via MSN to Forbairt Send a message via Yahoo to Forbairt Send a message via Skype™ to Forbairt
Default

BTW I should add I don't like what you're doing in your code


Code:
$format[1]  = "PC";
$format[2]  = "Xbox";
$format[3]  = "Xbox360";
$format[4]  = "PS2";
$format[5]  = "PS3";
$format[6]  = "PSP";
$format[7]  = "GC";
$format[8]  = "Wii";
$format[9]  = "DS";
$format[10] = "GBA";   

$formats = ";1|;3|;7|";

$string = "";
preg_match_all("/(\d+)/",$formats,$matches);
$items=$matches[1];
sort($items);

foreach($items as $console)
{
  $string .= $format[$console]." "; 
}
chop($string);

echo $string;
Multiple if statements like that are really a no no
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
array, string

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Adding to a string in PHP???? ziycon Coding Help 5 28-09-2007 07:13 PM
PHP string infected with weird characters. littleBird Coding Help 5 10-03-2007 09:19 AM
Can I get a variable name as a string? montyauto Coding Help 5 25-02-2007 10:41 PM
clean up a string (123..9) louie Coding Help 1 11-01-2007 12:58 PM


Sponsored links

Paid On Results