preg_replace, more than 4

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Thu Mar 10, 2011 8:06 pm

Good to use str_replace() four times

tkdbb84
New php-forum User
New php-forum User
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Fri Mar 11, 2011 6:58 am

For what purpose are you trying to make these replacements? There is probably a better way to go about solving the problem as a whole.

The problem with using string replacement or reg. exp is that "0" is a substring of "00" like "000" is a substring of "00000", so the lower number of zero's is going to be replaced regardless, your going to have to build a full function that checks the next.next.next.next.next to see if they are all zeros, then replace after the fact. Sounds almost like something i would assign to see if people who are newer to programing have a firm grasp on looping and logic.

however ignoring my suspicions i'll give you a push in the right direction.

I would setup a boolean flag something like $nonzero_found, iterate through the string 5 char's at a time, so:
0 to 4, 1 to 5, 2 to 6, 3 to 7... n to n+4, if the 1st char is not a 0, i'll skip to the next set, because we are not changing anything at that point, if the 1st (n) is a 0, i'll check n+1 if its a 0, i'll check n+2, if it is also a 0, i'll check n+3...... and repeat until i get to the last char, if the last char is also 0, i'll skip to the next iteration, if any of the char's including the last or previous are not 0s, i'll set all the known zeros to "1".

I'm not writing out the code for you. Everything you need is there.

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Fri Mar 11, 2011 9:25 am

Yes - str_replace() will not work because '0' is a substring of '00' and so on.... You have to go char by char in that string....

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Fri Mar 11, 2011 9:36 am

As you have fewer number of replacements (less than 5),you can also try like this:

$original_str = "11000001100010011000001";
$your_result = "11000001111111111000001";

$my_result = str_replace('100001','111111',$original_str);
$my_result = str_replace('10001','11111',$my_result);
$my_result = str_replace('1001','1111',$my_result);
$my_result = str_replace('101','111',$my_result);

echo "\n Original String = ".$original_str;
echo "\n My Result = ".$my_result;
echo "\n Your Result = ".$your_result;

output

Code: Select all

Original String  = 11000001100010011000001
My Result       =  11000001111111111000001
Your Result     = 11000001111111111000001

tkdbb84
New php-forum User
New php-forum User
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Fri Mar 11, 2011 10:35 am

clever JohnJ, i didn't think about adding 1s as terminators to the search parameters.

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Fri Mar 11, 2011 10:43 am

johnj's answer works for most cases except in the case where the sting of zeros either starts or ends the string...

this can be rectified by parsing a "1" to the start and end of the string and then removing them after the processing is complete.

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Fri Mar 11, 2011 11:16 am

@tkdbb84

You seem to be correct about the homework thing...

@dreami0

You may be incorrect in your description


http://www.daniweb.com/web-development/ ... ads/304442

tkdbb84
New php-forum User
New php-forum User
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Fri Mar 11, 2011 11:34 am

Ha ha, same exact problem about 7 months ago, it's been a while, but isn't that about the length of time between semesters.

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Fri Mar 11, 2011 11:48 am

That being said... just to flash my e-peen I figured out how to do it with preg_replace_callback

Code: Select all

$last = "";
$result = "11000001100010011000001";
while($last != $result){
	$last = $result;
	$result = preg_replace_callback('/(1|^)(0{1,4})(1|$)/',match_length,$last);
}
echo $result;

function match_length($matches){
	$r = str_repeat('1', strlen($matches[0]));
	return $r;
}
As long as preg_replace_callback finds a match the loop executes again. Match Length grabs the length of the match and returns a string of 1's as long as the string of zero's surrounded by 1's (i.e. match on 10001 returns 11111)

8) 8) 8)

johnj
php-forum Super User
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm

Fri Mar 11, 2011 9:10 pm

Yes, Nullsig is right - my trick will not work at the beginning and end of the string :D . dreami0 - please follow what Nullsig has done using callback or use what tkdbb84 has suggested- both are equally good. Most of the time you really do not know the result (result is used in callback as a terminating condition). In that case, solution suggested by tkdbb84 looks better.

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Sun Mar 13, 2011 8:36 am

Both those were mine, actually. TKDBB was just quoting my post.

Post Reply