Solved

Array Replacing loop PHP

Posted on 2011-09-19
3
192 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
Comment Utility
$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
Comment Utility
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
Comment Utility
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 Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now