Separate string without comma in PHP [duplicate] - php

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 9 years ago.
My original string is
one,two,three,four,five,
I need separate each word as a link
one, two, three, ...
My code is
$plat = $row['reg'];
foreach ($plat as $key => $pv) {
$pl[] = implode(',', $pv);
}
for ($p = 1; $p = sizeof($pl); $i++) {
echo '' . $pl[i] . '';
}

This would suffice..
<?php
$str='one,two,three,four,five';
$arr=explode(',',$str);
foreach($arr as $val)
{
echo "<a href=''>$val</a>, ";
}
OUTPUT :
<a href=''>one</a>, <a href=''>two</a>, <a href=''>three</a>, <a href=''>four</a>, <a href=''>five</a>

<?php
$str='one two three four five';
$arr=explode(' ',$str);
foreach($arr as $val)
{
echo "<a href=''>$val</a>, ";
}
this Code to separate blank spacesepration

$string = 'one,two,three,four,five';
$tag_open = '<a href="#" rel="tag">';
$tag_close = '</a>';
echo $tag_open. implode($tag_close.', '.$tag_open, explode(',', $string)). $tag_close;

Try this code:
$plat=$row['reg'];
foreach ($plat as $key=> $pv) {
$pl[] = explode(',', $pv);
for($p=1;$p=sizeof($pl); $i++) {
echo ''.trim($pl[i]).'';
}
}

Related

How to remove the last comma from foreach loop?

I am trying to remove the last comma(,) from foreach loop in php with the following code
<?php
foreach ($snippet_tags as $tag_data) {
$tags_id = $tag_data->tag_id;
$tagsdata = $this->Constant_model->getDataOneColumn('tags', 'id', $tags_id);
$tag_name=$tagsdata[0]->tag_name;
?>
<?php echo $tag_name; ?> ,
<?php }
?>
Right I am getting result like
Hello, How, sam,
But i wants to remove the last comma
By placing the HTML in a simple string variable and then using rtrim() on the resulting string before outputting it this should remove the final , from the string
<?php
$out = '';
foreach ($snippet_tags as $tag_data) {
$tags_id = $tag_data->tag_id;
$tagsdata = $this->Constant_model->getDataOneColumn('tags', 'id', $tags_id);
$tag_name=$tagsdata[0]->tag_name;
// move inside loop and amend to place in a simple string var
$out .= '' . $tag_name . ',';
?>
echo rtrim($out, ',');
You can also use the following code -
<?php
$numItems = count($snippet_tags);
$i = 0;
foreach ($snippet_tags as $tag_data) {
$tags_id = $tag_data->tag_id;
$tagsdata = $this->Constant_model->getDataOneColumn('tags', 'id', $tags_id);
$tag_name=$tagsdata[0]->tag_name;
?>
if(++$i === $numItems)
echo "<a href='base_url() ?>tags/<?php echo $tag_name;'> $tag_name</a>";
else echo "<a href='base_url() ?>tags/<?php echo $tag_name;'> $tag_name</a> ,";
<?php
}
?>

PHP Get variable explode

I am using this code for explode and show GET variables. But I would like remove current query in the link:
My explode code:
$k = $_GET['sef'];
$s_explode = explode("-",$k);
foreach($s_explode as $q) {
if($q==$s_explode[0]) {
echo '<a class="active" href="/category/'.$q.'">'.$s_explode[0].' <span class="dismiss">×</span></a>';
} else {
echo ''.$q.' <span class="dismiss">×</span>';
}
}
If I using GET
website.com/?sef=game-book-video
Print is:
<a class="active" href="/category/game">game</a>
book
video
I would like if I using GET
website.com/?sef=game-book-video
<a class="active" href="/category/book-video">game</a>
book
video
I hope I can explain good sorry for my bad English.
Your code would be like this:
$k = $_GET['sef'];
$s_explode = explode("-", $k);
//game-book-video
foreach($s_explode as $i => $q) {
$parts = $s_explode;
if(($key = array_search($q, $parts)) !== false) {
unset($parts[$key]);
}
$class = ($i == 0 ? "class='active'" : '');
echo '<a ' . $class . ' href="/category/'.implode('-', $parts).'">'.$q.' <span class="dismiss">×</span></a>';
}

How do i display list items from php array

Ive been trying make this display as html list items it just a string that i explode then loop over each item i cant get it to out put correctly. Could some one please show me where im going wrong or suggest an new approch.
this is what ive tried
$path = "1/2/3/4";
$expath = explode("/",$path);
$ret = '';
echo '<ul>';
foreach ($expath as $pitem) {
echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
}
echo '</ul>';
.
Desired out put on hrefs
1
1/2
1/2/3
1/2/3/4
Desired visual out LIs
1
2
3
4
Output i get be warned
1
12/>212/>23/>312/>23/>34/>4
$path = "1/2/3/4";
$expath = explode("/", $path);
echo '<ul>';
foreach ($expath as $i => $pitem) {
$slice = array_slice($expath, 0, $i + 1);
$path = implode('/', $slice);
echo '<li>' . $pitem . '</li>';
}
echo '</ul>';
$list = explode("/", "1/2/3/4");
This will create an array $list as:
echo $list[0]; // 1
echo $list[1]; // 2
echo $list[2]; // 3
echo $list[3]; // 4
This line is the problem: echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
Should be formatted like:
echo "<li><a href='{$ret}={$pitem}/'>{$pitem}</a></li>";
or echo '<li>'.$pitem.'</li>';
Its because your $ret. Place that inside the loop. In your code you concatenate $pitem with $ret all older $ret values also get concat.
Try
<?php
$path = "1/2/3/4";
$expath = explode("/",$path);
echo '<ul>';
foreach ($expath as $pitem) {
$ret = '';
echo '<li><a href='.$ret .= $pitem. "/".'>'.$pitem.'</a></li>';
}
echo '</ul>';
If you want = sign tobe there in the url then just change echo by following
echo "<li><a href='$ret=$pitem/'>$pitem</a></li>";
PHP echo with double quotes will print variable value.

Explode to array and print each element as list item

I have a set of numbers in a table field in database, the numbers are separated by comma ','.
I am trying to do the following:
Step 1. : SELECT set of numbers from database and explode it to array :
$array = explode(',', $set_of_numbers);
Step 2. : Print each element of the array as list item by using foreach loop :
foreach ($array as $list_item => $set_of_numbers){
echo "<li>";
print_r(array_list_items($set_of_numbers));
echo "</li>";}
Please anybody tell me what is wrong. Thank you.
$numbers = '1,2,3';
$array = explode(',', $numbers);
foreach ($array as $item) {
echo "<li>$item</li>";
}
Assuming your original $set_of_numbers is simply a CSV string, something like 1,2,3,4,..., then your foreach is "mostly" ok. But your variable naming is quite bonkers, and your print-r() call uncesary:
$array = explode(',', $set_of_numbers);
foreach($array as $key => $value) {
echo "<li>$key: $value</li>";
}
Assuming that 1,2,3,4... string, you'd get
<li>0: 1</li>
<li>1: 2</li>
<li>2: 3</li>
etc...
$numbers = "1,2,3";
$array = explode(",", $numbers);
/* count length of array */
$arrlength = count($array);
/* using for while */
$x = 0;
while ($x < $arrlength) {
echo "<li>$array[$x]</li>" . PHP_EOL;
$x++;
}
echo PHP_EOL;
/* using for classic */
for ($x = 0; $x < $arrlength; $x++) {
echo "<li>$array[$x]</li>" . PHP_EOL;
}
echo PHP_EOL;
/* using for each assoc */
foreach ($array as $value) {
echo "<li>$value</li>" . PHP_EOL;
}
echo PHP_EOL;
/* using for each assoc key */
foreach ($array as $key => $value) {
echo "<li>$key => $value</li>" . PHP_EOL;
}
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/ZqT4Yi" ></iframe>
You actually don't need to explode on the commas. You can just replace each comma with an ending tag followed by an opening tag and then wrap your whole string in an opening and closing tag.
$set_of_numbers = '1,2,3';
echo '<li>' . str_replace(',', '</li><li>', $set_of_numbers) . '</li>';
// outputs: <li>1</li><li>2</li><li>3</li>
No looping is necessary.
Here is answer for your question to get ride of your problem
$Num = '1,2,3,4,5,';
$Array = explode(',',$Num);
foreach ($Array as $Items)
{
echo "<li>&Items</li>"; // This line put put put in the list.
}
This can easily be achieved by the following code snippet:
<?php
$my_numbers = '1,12,3.2,853.3,4545,221';
echo '<ul>';
foreach(explode(',', $my_numbers) AS $my_number){
echo '<li>'.$my_number.'</li>';
}
echo '</ul>';
The above code will output the following HTML:
<ul><li>1</li><li>12</li><li>3.2</li><li>853.3</li><li>4545</li><li>221</li></ul>
Credits: http://dwellupper.io/post/49/understanding-php-explode-function-with-examples

Print in foreach with comma [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Removing last comma in PHP?
So I have the following code:
<?php
foreach ($movie->Genres as $genre):
?>
<?=$genre->name?>,
<?php
endforeach;
?>
And this will give me the following: Action, Drama, Crime, but I don't need this last comma separator - so my question is what would be the best way to avoid that comma ?
Ps.
If that matters I have 4 loops like this one on my page - for actors, directors, etc.
Write you A-tags to an array and join the items.
<?php
$items = array();
foreach ($movie->Genres as $genre):
$items[] = '' . $genre->name . '';
endforeach;
echo join(', ', $items);
?>
$temp = array();
foreach($movie->Genres as $genre) {
$temp[] = <<<EOL
{$genre->name}
EOL;
}
echo implode(',', $temp);
The other option is to use string concatenation, then a substring to delete the trailing comma:
$temp = '';
foreach(...) {
$temp .= ...
}
echo rtrim($temp, ',');
$a = array();
foreach ( $movie->Genres as $v ){
$a[] = '' . $v->name . '';
}
$s = implode(',', $a);
var_dump($s);

Categories