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
Solved

Array Replacing loop PHP

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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 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…

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