Solved

Array Replacing loop PHP

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

Technology Partners: 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

726 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