Page 1 of 1
string replace issue
Posted: Wed Nov 30, 2005 12:35 am
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.
Posted: Wed Nov 30, 2005 9:09 am
by dyonak
You should be able to use a regex to do this.
Posted: Wed Nov 30, 2005 9:30 am
by shiznatix
Code: Select all
$replaced = str_replace(strtolower($replace_this), $with_this, strtolower($in_this_string));
Posted: Thu Dec 01, 2005 1:47 am
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.
Posted: Thu Dec 01, 2005 4:25 am
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;
}
}
Posted: Mon Dec 05, 2005 12:34 am
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....
?