string replace issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

string replace issue

Post by itsmani1 »

hi there....

i need some function tht gives me replacement of strings with out taking care of their case, i can't use str_ireplace (Case-insensitive version of str_replace()) because i hav't got php5 i have 4.1.1 right now . is there any replacement tht can help me out.


thanx.
Mannna.
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

You should be able to use a regex to do this.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

$replaced = str_replace(strtolower($replace_this), $with_this, strtolower($in_this_string));
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

well this will convert whole string to lower-case this not a solution.
wot is required is :
if one char is in lower of upper string it should do the work...



Mannan.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

include this in your code (before the use of str_ireplace is required):

Code: Select all

if (!function_exists('str_ireplace')) {
    function str_ireplace($pat, $rep, $str)
    {
		if ((is_array($pat)) && (is_array($rep))) {
			$c = count($pat);
			for ($i = 0; $i < $c; $i++) {
				$str = preg_replace('/' . preg_quote($pat[$i], '/') . '/i', $rep[$i], $str);
			}
		} elseif ((is_array($pat)) && (is_string($rep))) {
			foreach ($pat as $p) {
				$str = preg_replace('/' . preg_quote($p, '/') . '/i', $rep, $str);
			}
		} elseif ((is_string($pat)) && (is_string($rep))) {
				$str = preg_replace('/' . preg_quote($pat, '/') . '/i', $rep, $str);
		} else {
			return false;
		}
		return $str;
    }
}
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

can any one help me in string padding i want to padd a string on both sides of string.


for example
i have a string =" This is my String";
now what i want is : to pass "_" on both sides of s and S
howz this possible....

?
Last edited by itsmani1 on Mon Dec 05, 2005 12:40 am, edited 3 times in total.
Post Reply