Solved

php add string in string

Posted on 2013-02-05
10
268 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 31

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 43

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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 31

Expert Comment

by:Marco Gasi
ID: 38856044
@Slimshaneey: good point! Concise and elegant: I like it.
0
 

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 43

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 43

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

697 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