Solved

php add string in string

Posted on 2013-02-05
10
265 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

867 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

14 Experts available now in Live!

Get 1:1 Help Now