Solved

Array Replacing loop PHP

Posted on 2011-09-19
3
211 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
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
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…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 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