Solved

php add string in string

Posted on 2013-02-05
10
267 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to dynamically set the form action using jQuery.

860 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