Solved

php add string in string

Posted on 2013-02-05
10
264 Views
Last Modified: 2013-02-06
say i have string : "red white green"

i would like to make the string become below in dynamic

<li style="color:red"></li><li style="color:white"></li><li style="color:green"></li>

expert, how am i dynamic do this in php
0
Comment
Question by:john80988
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38855474
$str = "red white green";
$arr = explode(' ', $str);
//now the array is like this:
//array([0] => "red", [1]=>=>"white", [2]=>"green")

$result = <li style="color:' . $arr[0] . '"></li><li style="color:' . $arr[1] . '"></li><li style="color:' . $arr[2] . '"></li>';

echo $result;

Cheers
0
 

Author Comment

by:john80988
ID: 38855504
"red white green"; is example, it may be less or more than 3 value

maybe need foreach to , but how?
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 38855612
Here it is with foreach:

<?php 
$str = "red white green";
$arr = explode(' ', $str);

foreach ($arr as $color) {
	printf('<li style="color:%s"></li>',$color);
}
?>

Open in new window

0
 
LVL 11

Accepted Solution

by:
Slimshaneey earned 500 total points
ID: 38855801
You dont need a foreach, an implode will do here:

$str = "red white green";
$arr = explode(' ', $str);

$output = '<li style="color:' . implode('"></li><li style="color:', $arr) . '"></li>';

echo $output;

Open in new window

0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 38856044
@Slimshaneey: good point! Concise and elegant: I like it.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:john80988
ID: 38857689
the $ouput result in <li style="color:red blue green"></li>

what i need is <li style="color:red"></li><li style="color:blue"></li><li style="color:green"></li>
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 38857725
Post your code!

FWIW - I prefer the foreach(). I think it's cleaner than the implode (plus it's easier to read and debug). You certainly won't get the output that you say you're getting. Give it a try and see how you get on.
0
 

Author Comment

by:john80988
ID: 38857732
foreach ($arr as $color) {
      printf('<li style="color:%s"></li>',$color);
}

how do i store it into one long string -  <li style="color:red"></li><li style="color:blue"></li><li style="color:green"></li>
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 38857739
If you want to add it to a string, just concantenate your variable, but if you want to output it, why bother - just output it line by line. Building a variable seems an unnecessary step, but here you go anyway.

$output = '';
foreach ($arr as $color) {
      $output .= sprintf('<li style="color:%s"></li>',$color);
}
echo $output;

Open in new window

0
 

Author Comment

by:john80988
ID: 38857792
i found the problem, original string output to webpage is like that

<center>red
blue
green</center>

mean it got more space between each wording, this make code below malfunction

$arr = explode(' ', $str);

the space may be more than 1 spaces. how do i get rid of this problem?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now