Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exploding string from array while in loop syntax problem 2

Status
Not open for further replies.

topjimmie

IS-IT--Management
Feb 4, 2002
28
US
I am trying to build a test to explode each string from an array while doing foreach loop.
Can someone show me what I'm doing wrong with the array and construction of the statement. All I get in return is 'array'.

Code:
$posted = array('text1@url1', 'text2@url2', 'text3@url3');

Foreach($posted as $astring)
{
	$astring = explode("@", $posted);
			$adesc = $astring[0];
			$aurl = $astring[1];

echo $adesc, ' then ', $aurl;
}

More info in case you have input on how this whole thing might be handled better:
The real array is coming from POST data which are comma delimited strings. I need to explode the string at the '@' and assign to 2 variables. These variables then become Cell values in a PDF doc that this code is for (using TCPDF).
So, I need to loop through the array, each time assigning the 2 resulting variables to a line in the pdf file.

If you can just help me with the test that would be a HUGE assist!
Thanks
 
You are exploding your array $post,

perhaps what you mean to explode is each of the items in your $post array.

In which case you want to explode the $astring variable and assign it to a new variable.

Code:
Foreach($posted as [blue]$astring[/blue])
{
    [red]$differentstring[/red] = explode("@", [blue]$astring[/blue]);
            $adesc = $[green]differentstring[/green][0];
            $aurl = $[green]differentstring[/green][1];

echo $adesc, ' then ', $aurl;
}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you Vacunita, that is exactly what I was trying to do! Amazing how the simple things can get really convoluted
 
Hi

By the way, if you really not need the $differentstring array, as in your example, you could use [tt]list()[/tt] :
PHP:
[b]foreach[/b] [teal]([/teal][navy]$posted[/navy] [b]as[/b] [navy]$astring[/navy][teal])[/teal] [teal]{[/teal]
  [b]list[/b][teal]([/teal][navy]$adesc[/navy][teal],[/teal][navy]$aurl[/navy][teal])[/teal] [teal]=[/teal] [COLOR=darkgoldenrod]explode[/color][teal]([/teal][green][i]"@"[/i][/green][teal],[/teal] [navy]$astring[/navy][teal]);[/teal]
  [b]echo[/b] [navy]$adesc[/navy][teal],[/teal] [green][i]' then '[/i][/green][teal],[/teal] [navy]$aurl[/navy][teal];[/teal]
[teal]}[/teal]
But if your only goal is to obfuscate abit the email address before [tt]echo[/tt]ing it out, this does the same :
PHP:
[b]foreach[/b] [teal]([/teal][navy]$posted[/navy] [b]as[/b] [navy]$astring[/navy][teal])[/teal]
  [b]echo[/b] [COLOR=darkgoldenrod]str_replace[/color][teal]([/teal][green][i]'@'[/i][/green][teal],[/teal][green][i]' then '[/i][/green][teal],[/teal][navy]$astring[/navy][teal]);[/teal]

Feherke.
 
Thanks feherke, that helps! I've run into a problem related to this post. I was trying to do a test just to fill out the PDF file I'm building by using an array I made up. The idea then was to replace that with the _POST array from my form. I kept getting errors thrown up when using Foreach, so I checked to see what is being POSTed and it's not what I expected.

I'm getting:

Array ( [0] => 17:00@ 17:00@ 17:00@ )

I think I need to explode the POSTed data at each comma and put each value into an array so I can use Foreach.

Am I on the right track?
Thanks again for your help-I'm learning, but the task at hand is a little beyond my knowledge sometimes.
 
Hi

Is that the output of [tt]print_r[teal]([/teal][navy]$_POST[/navy][teal]);[/teal][/tt] ? Looks abit strange for me.

Anyway, if you have the list elements enumerated in one string, yes, you will have to split it. ( Personally I put it directly into the [tt]foreach[/tt]. Readability can be discussed, but hopefully we will skip it. )
PHP:
[b]foreach[/b] [teal]([/teal][COLOR=darkgoldenrod]explode[/color][teal]([/teal][green][i]','[/i][/green][teal],[/teal][navy]$_POST[/navy][teal][[/teal][purple]0[/purple][teal]])[/teal] [b]as[/b] [navy]$astring[/navy][teal])[/teal] [teal]{[/teal]
  [b]list[/b][teal]([/teal][navy]$adesc[/navy][teal],[/teal][navy]$aurl[/navy][teal])[/teal] [teal]=[/teal] [COLOR=darkgoldenrod]explode[/color][teal]([/teal][green][i]"@"[/i][/green][teal],[/teal] [navy]$astring[/navy][teal]);[/teal]
  [b]echo[/b] [navy]$adesc[/navy][teal],[/teal] [green][i]' then '[/i][/green][teal],[/teal] [navy]$aurl[/navy][teal];[/teal]
[teal]}[/teal]
In this cases, when the data format is not guaranteed ( the user may put spaces after the comma, or not, or even in front of the comma ), I prefer to use [tt]preg_split()[/tt] instead, as [tt]preg_split[teal]([/teal][green]'/\s*,\s*/'[/green][teal],[/teal][navy]$_POST[/navy][teal][[/teal][purple]0[/purple][teal]])[/teal][/tt]. Of course, you can just [tt]trim()[/tt] the leading/trailing space(s), is matter of personal preference.

Feherke.
 
Sorry, I didn't provide a very good example. You are correct about it not looking right. Here is the code with key, and result:
Code:
print_r($_POST['listshift']);
And this is what is returned:
17:00@ 17:00@ 17:00@
These is an extremely long string, so I have truncated part of each one (the truncated parts are the rest of the url and unique identifier.)

Again, I apologize for not explaining better.

From this then, I need to build an array from $_POST['listshift']

Trying your posted code out to see where it ends up.
Thanks!!
 
feherke, I think it's working!! THANK YOU (I've been trying to figure this out for many hours!)
I'll throw it in the script and hopefully it will work like expected
Here is what I ended up with just in case another person is looking for the same thing:
Code:
foreach (explode(',',$_POST['listshift']) as $astring) {
  list($adesc,$aurl) = explode("@", $astring);
  echo $adesc, ' then ', $aurl;
}
Good point about the use of preg split too!
Thanks a million for all of the help with this feherke and Vacunita.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top