Jump to content

Recommended Posts

I have a group of links but need to add a number next to each

 

$links = "http://site.com<br>
            http://site2.com<br>
            http://site3.com<br>"; //etc..up until 25 links

 

I thought of using strreplace and explode but they will both replace - for example the <br> with the same number, but i need to count how many <br> tags there are in the variable and add a niumber next to each

 

So basically i need the output to be:

 

$links = "http://site.com<br> 1
            http://site2.com<br> 2
            http://site3.com<br> 3"; //etc..up until 25 links

Link to comment
https://forums.phpfreaks.com/topic/166073-help-with-variable-loop/
Share on other sites

$temp = array();
$links = explode('<br>', $links);
$number = 1;
foreach ($links as $link) {
    $link = trim($link); // tabs, newlines, .. as noted on: http://us2.php.net/manual/en/function.trim.php
    $temp[] = $link . nl2br("\n") . $number++;
}

$links = implode('', $temp);

If each url is followed by a <br>, you can explode by the <br>, then loop through the array to add it back in with the number:

 

$linksArray = explode('<br>', $links);
$counter = 1;
$result = '';

foreach ($linksArray as $l) {
    $result .= $l . '<br> ' . $counter . ' ';
    $counter++;
}

I cant figure out whats going on with this, the out put is:

 

2 loops

http://site.com
http://site2.com
http://site3.com
http://site.com
1 http://site2.com
2 http://site3.com
3
4 http://site.com
http://site2.com
http://site3.com
http://site.com
1 http://site2.com
2 http://site3.com
3
4

 

The code i used is:

 

$links = "http://site.com<br>
            http://site2.com<br>
            http://site3.com<br>";

$linksArray = explode('<br>', $links);
$counter = 1;
$result = '';

foreach ($linksArray as $l) {
    $links .= $l . '<br> ' . $counter . ' ';
    $counter++;
}
echo $links;

 

For 2 loops the out put should be:

 

http://site.com<br> 1

http://site2.com<br> 2

$links = "http://site.com<br>
            http://site2.com<br>
            http://site3.com<br>";

$linksArray = explode('<br>', $links);
$counter = 1;
$links = '';

foreach ($linksArray as $l) {
    if ($l != '') {
        $links .= $l . '<br> ' . $counter . ' ';
        $counter++;
    }
}
echo $links;

 

You have to reset $links before adding the result.  Also, if you don't want the last blank line, check to see if it is an empty string.

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.