Solved

Array Replacing loop PHP

Posted on 2011-09-19
3
233 Views
Last Modified: 2012-05-12
I want to do something like

$string = "? is ? but ? do ? know how to make it work.";
$array = array("This", "cool", "I", "not");
// $result = str_replace or preg_replace.. idk here

echo $result;

Open in new window


Result: This is cool but I do not know how to make it work.


Is this possible?
0
Comment
Question by:Phelms215
[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
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 36564620
$string = "? is ? but ? do ? know how to make it work."; 
$array = array("This", "cool", "I", "not");  
foreach ($array as &$value) {
    $value = $value * 2;
	$result = preg_replace('/?/', $value,$string,1)
}

Open in new window

0
 
LVL 23

Accepted Solution

by:
Kamaraj Subramanian earned 500 total points
ID: 36564624
oops, sorry,

remove the below code

$value = $value * 2;  

Open in new window


$string = "? is ? but ? do ? know how to make it work.";   
$array = array("This", "cool", "I", "not");    
foreach ($array as &$value) {  
        $result = preg_replace('/?/', $value,$string,1) ;
} 
echo $result;

Open in new window

0
 
LVL 8

Expert Comment

by:PranjalShah
ID: 36564644
Is your original string contains only one character that needs to be changed to different values? I am not sure if you can do that. But if your $string is

$string = "? is * but $ do # know how to make it work";
$search = array("?" , "*" , "$", "#");
$replace = array("This", "cool", "I", "not");
$new_string = str_replace($search, $replace, $string);

Open in new window

0

Featured Post

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

Question has a verified solution.

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

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

691 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