dreamwest Posted July 15, 2009 Share Posted July 15, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166073-help-with-variable-loop/ Share on other sites More sharing options...
ignace Posted July 15, 2009 Share Posted July 15, 2009 $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); Quote Link to comment https://forums.phpfreaks.com/topic/166073-help-with-variable-loop/#findComment-875834 Share on other sites More sharing options...
J.Daniels Posted July 15, 2009 Share Posted July 15, 2009 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/166073-help-with-variable-loop/#findComment-875835 Share on other sites More sharing options...
dreamwest Posted July 16, 2009 Author Share Posted July 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166073-help-with-variable-loop/#findComment-876152 Share on other sites More sharing options...
J.Daniels Posted July 16, 2009 Share Posted July 16, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/166073-help-with-variable-loop/#findComment-876160 Share on other sites More sharing options...
dreamwest Posted July 16, 2009 Author Share Posted July 16, 2009 Thanks. Go it to works Quote Link to comment https://forums.phpfreaks.com/topic/166073-help-with-variable-loop/#findComment-876184 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.