Solved

php add string in string

Posted on 2013-02-05
10
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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.

688 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