phoenixx Posted November 17, 2010 Share Posted November 17, 2010 I have the following code which has deliimiters of |||| and I need to break it apart into three strings and have a variable assigned to each string. I've tried exploding it but won't dynamically increase the variable. I know at one point about a year ago I used something like $var.=$foo and the $var would increase $var1, $var2, etc... Here's the string I need to break.j (And the string will be different every time, and some will have 2 string delimited some will have 20, and some inbetween. The rich contemporary style of the "Theo" Counter Height Table combines faux marble and a warm finish to create dining room furniture that adds an exciting style to the decor of any home. The thick polyurethane coated faux marble table top perfectly accentuates the warm brown finish flowing over the straight-lined contemporary design of the apron and legs to help create an exceptional dining experience. With the beautiful stitching and button tufting details of the faux leather upholstered bar stools, the "Theo" Counter Height Table is a refreshing addition to any home.||||Table top made with polyurethane coated print marble. Aprons and legs made from select veneer and solids with a warm brown finish. Chair is upholstered in a brown PVC with accent top stitching. D158-233 bar stool dimension: 18"W x 21"D x 40"H.||||Click here for complete image download listing for series D158. In a perfect world it would be: $desription1="The rich contemporary style of the "Theo" Counter Height Table combines faux marble and a warm finish to create dining room furniture that adds an exciting style to the decor of any home. The thick polyurethane coated faux marble table top perfectly accentuates the warm brown finish flowing over the straight-lined contemporary design of the apron and legs to help create an exceptional dining experience. With the beautiful stitching and button tufting details of the faux leather upholstered bar stools, the "Theo" Counter Height Table is a refreshing addition to any home."; $description2="Table top made with polyurethane coated print marble. Aprons and legs made from select veneer and solids with a warm brown finish. Chair is upholstered in a brown PVC with accent top stitching. D158-233 bar stool dimension: 18"W x 21"D x 40"H.": $description3="Click here for complete image download listing for series D158."; Quote Link to comment https://forums.phpfreaks.com/topic/218981-exploding-a-string-into-three-variables/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 17, 2010 Share Posted November 17, 2010 The reason explode produces an array is because it is easier to work with an array when you have related sets of data. $data = " your data here ..."; $description = explode('||||',$data); At this point you will have the variables: $description[0], $description[1], $description[2], ... up to $description[n] You can use array functions, such as foreach() and count() to iterate over each element of the array or get a count of how many elements there are. Quote Link to comment https://forums.phpfreaks.com/topic/218981-exploding-a-string-into-three-variables/#findComment-1135625 Share on other sites More sharing options...
ManiacDan Posted November 17, 2010 Share Posted November 17, 2010 Note that your original post proved why your example is NOT a perfect world. You typed 3 variable names, and managed to spell one of them wrong. If you used a loop and an array, the risk of you misspelling the variable name is greatly reduced. If you are writing scripts with variable names that are all the same except for the number at the end, you're doing it wrong. Arrays are the most powerful feature of PHP, use them. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/218981-exploding-a-string-into-three-variables/#findComment-1135646 Share on other sites More sharing options...
phoenixx Posted November 17, 2010 Author Share Posted November 17, 2010 Okay, sorry - here is a better breakdown of the scenario. I have scraped data using the following code: preg_match_all('~<td\s+width="550">(.*?)</td>~', $data, $series_description); // Series Descriptions $d = array_merge($series_description[0]); foreach($d as $k=>$v){ echo $v . "||||"; } In this situation it spits out : The rich contemporary style of the "Theo" Counter Height Table combines faux marble and a warm finish to create dining room furniture that adds an exciting style to the decor of any home. The thick polyurethane coated faux marble table top perfectly accentuates the warm brown finish flowing over the straight-lined contemporary design of the apron and legs to help create an exceptional dining experience. With the beautiful stitching and button tufting details of the faux leather upholstered bar stools, the "Theo" Counter Height Table is a refreshing addition to any home.||||Table top made with polyurethane coated print marble. Aprons and legs made from select veneer and solids with a warm brown finish. Chair is upholstered in a brown PVC with accent top stitching. D158-233 bar stool dimension: 18"W x 21"D x 40"H.||||Click here for complete image download listing for series D158.|||| I need to separate each $v into it's own dynamically increasing variable so that it can be written to the database. Quote Link to comment https://forums.phpfreaks.com/topic/218981-exploding-a-string-into-three-variables/#findComment-1135652 Share on other sites More sharing options...
PFMaBiSmAd Posted November 17, 2010 Share Posted November 17, 2010 In a way, that is really really funny ^^^. The separator characters you implied as being part of the data is simply something you echoed between each piece of data as you were iterating over it from the array it is already in. Your data is already in an array. You are already iterating over it using a foreach() loop. ALL YOU NEED TO DO is INSERT the data into your database table inside of the foreach() loop you already have. Quote Link to comment https://forums.phpfreaks.com/topic/218981-exploding-a-string-into-three-variables/#findComment-1135659 Share on other sites More sharing options...
phoenixx Posted November 17, 2010 Author Share Posted November 17, 2010 The data needs to be worked into other aspects of the script in the page, and then finally will be inserted into the database. I'll work with it. NOT SOLVED BUT CLOSED. Quote Link to comment https://forums.phpfreaks.com/topic/218981-exploding-a-string-into-three-variables/#findComment-1135663 Share on other sites More sharing options...
ManiacDan Posted November 17, 2010 Share Posted November 17, 2010 Not really closed, either. You have an array. The array is $series_description[0] (and then, for some reason, $d). USE THAT ARRAY. Read the PHP manual on arrays. Arrays are lists of data. They are designed specifically for what you're asking to do. We're not being mean by pointing out their existence. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/218981-exploding-a-string-into-three-variables/#findComment-1135738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.