Jump to content

Recommended Posts

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.";

Link to comment
https://forums.phpfreaks.com/topic/218981-exploding-a-string-into-three-variables/
Share on other sites

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.

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

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.

 

 

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.

Not really closed, either.    ;D

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.