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?

Recommended Answers

All 4 Replies

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

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 />';
}

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>';
 }

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.