A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearMultiple string replacements in the same string in php - page 1
User InfoPosts
Multiple string replacements in the same string in php#1
I have the following string replacement problem and I am in quite a fix here

PFB the sample string

$string = (The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog.(;

$patterns[0] = (/quick/(;
$patterns[1] = (/quick/(;
$patterns[2] = (/fox/(;

$replacements[2] = (bear(;
$replacements[1] = (black(;
$replacements[0] = (slow(;

echo preg_replace($patterns, $replacements, $string);


I need to replace (quick( depending on the numbers i send

i.e if my input to a function is 56, the quick before 56 needs to be replaced with bear and if my input to a function is 78, the quick before 78 needs to be replaced with black

Can someone please help me with this?

posted date: 2009-04-10 04:46:00


Re: Multiple string replacements in the same string in php#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-10 04:46:01


Re: Multiple string replacements in the same string in php#3
Instead of working with preg_replace, use substr_replace to do your string replacement and strpos to find the start and end points within the string based on the parameters you pass. Your pattern is a simple string, so it doesn(t require a regular expression, and substr_replace will allow you to specify a start and end point within the string to do replacements (which seems to be what you(re looking for).EDIT:Based on your comment, it sounds like you have to do a lot of checking. I haven(t tested this, so it may have a bug or two, but try a function like this:function replace($number, $pattern, $replacement){ $input = "The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog."; $end_pos = strpos($input, $number); $output = ""; if($end_pos !== false && substr_count($input, $pattern, 0, $end_pos)) { $start_pos = strrpos(substr($input, 0, $end_pos), $pattern); $output = substr_replace($input, $replacement, $start_pos, ($start_pos + strlen($pattern))); } return $output;}This function does the following:First, check that the "number" parameter even exists in the string ($end_pos !== false)Check that your pattern exists at least once in between teh beginning of the string and the position of the number (substr_count($input, $pattern, 0, $end_pos))Use strrpos function to get the position of the last occurrence of the pattern within the substringUse the start position and the length of the pattern to insert your replacement string using substr_replace

posted date: 2009-04-10 05:15:00


Re: Multiple string replacements in the same string in php#4
I think regular expressions will make this difficult but you should be able to do it using only strpos(), substr() and str_replace().Use strpos to find the location in the string of 56 and 78.Then cut the string up into substrings at these points using substr.Now, replace (quick( with the correct variable, depending on whether 56 or 78 was sent to the function and which substring you are dealing with.

posted date: 2009-04-10 05:16:00


Re: Multiple string replacements in the same string in php#5
You are doing it the wrong way. Instead depending on your function input you should use the correct find and replace values. Just create a map of find and replace values depending on your function input value. Like:$map = array( 56 => array((patterns( => array(), (replacements( => array()), 78 => array(...));

posted date: 2009-04-10 05:20:00


Re: Multiple string replacements in the same string in php#6
Try this:$searchArray = array("word1", "sound2", "etc3");$replaceArray = array("word one", "sound two", "etc three");$intoString = "Here is word1, as well sound2 and etc3";//now let(s replaceprint str_replace($searchArray, $replaceArray, $intoString);//it should print "Here is word one, as well sound two and etc three"

posted date: 2013-09-18 23:46:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.