0

I have this...

<?php
$links = explode("\n", $_POST['links']); 

foreach ($links as $links)
{
  echo '[PART="NUM"]'.$links.'[/PART] <br />';
}

?>

Where it says NUM I want the number of its position in the array.

So if I fed this to this script:

http://www.google.com http://www.yahoo.com http://www.ask.com

The script would then output:

[PART="1"]http://www.google.com[/PART]
[PART="2"]http://www.yahoo.com[/PART]
[PART="3"]http://www.ask.com[/PART]

How would I go about doing this?

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by almostbob
0

I have this...

<?php
$links = explode("\n", $_POST['links']); 

foreach ($links as $links)
{
  echo '[PART="NUM"]'.$links.'[/PART] <br />';
}

?>

Where it says NUM I want the number of its position in the array.

So if I fed this to this script:

http://www.google.com http://www.yahoo.com http://www.ask.com

The script would then output:

[PART="1"]http://www.google.com[/PART]
[PART="2"]http://www.yahoo.com[/PART]
[PART="3"]http://www.ask.com[/PART]

How would I go about doing this?

assuming the array is numeric (meaning keys are numeric starting from 0), here is your code:

foreach ($links as $key => $links)
{
  echo '[PART="'.$key.'"]'.$links.'[/PART] <br />';
}

enjoy

0

Like uncle_smith but
people tend to believe indexes should start at one

foreach ($links as $key => $links)
{
echo '[PART="'.$key+1.'"]'.$links.'[/PART] <br />';
}
0

Thanks :)

EDIT: Almostbob....

What you posted didn't work but someone else gave me this code which did.

$count = count($links);
for($i=0;$i<$count;$i++)
{
 echo '<p>[PART="'.($i+1).'"]'.$links[$i].'[/PART] <br /></p>';
 }
0

Thanks :)

EDIT: Almostbob....

What you posted didn't work but someone else gave me this code which did.

$count = count($links);
for($i=0;$i<$count;$i++)
{
 echo '<p>[PART="'.($i+1).'"]'.$links[$i].'[/PART] <br /></p>';
 }

bummer
appeared to work in my sandbox,
just threw values into an array and went from there
sorry
have clipped the code that works and put it in the library though
thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.