...

View Full Version : help in search and replace



bhakti_thakkar
11-13-2007, 05:45 AM
hi all,
i am having a string [eg: $str="The meeting is scheduled on ::date:: and at ::time::. The date and time may change to ::date:: and ::time:: or ::date:: and ::time:: ."].
time will have an array of hrours and minutes. i have an array of date
dispdate[] Array :
Array ( [0] => 15-11-2007 [1] => 15-11-2007 [2] => 20-11-2007 )

hours[] Array:
Array ( [0] => 11 [1] => 11 [2] => 11 )

minutes[] Array:
Array ( [0] => 30 [1] => 30 [2] => 30)

what i want is for first occurance of ::date:: shuld be replace with [0] element of dispdate[] array and the first occurance of time should be replaced with concatination of [0] element of hours[] and [0] element of minutes[] and second occurance of ::date:: should be replace with [1] element of dispdate[] and second occurance of ::time:: with concate of [1] element of hours[] and [1] element of minutes[] and so on....
i am tring it from past one day but just going nuts ... Please help me on this :eek:

how will i do it??
thanks....

bhakti_thakkar
11-13-2007, 06:27 AM
hi all,
i atlast found a function which did what i want :


function str_replace_once($search, $replace, $subject, &$offset = 0) {
if (is_array($search)) {
if (is_array($replace)) {
foreach ($search as $x => $value) $subject = str_replace_once($value, $replace[$x], $subject, $offset);
} else {
foreach ($search as $value) $subject = str_replace_once($value, $replace, $subject, $offset);
}
} else {
if (is_array($replace)) {
foreach ($replace as $value) $subject = str_replace_once($search, $value, $subject, $offset);
} else {
$pos = strpos($subject, $search, $offset);
if ($pos !== false) {
$offset = $pos+strlen($search);
$subject = substr($subject, 0, $pos) . $replace . substr($subject, $offset);
}
}
}

return $subject;
}
$sql="select TemplateName_VC, Header_Txt, Footer_Txt, Content_Txt from Templates_T where Template_ID='$Template_ID'";
$rs=$db->getRow($sql,DB_FETCHMODE_ASSOC);

$content=$rs['header_txt'].'<br>';
$content.=$rs['content_txt'].'<br>';
$content.=$rs['footer_txt'];
$Content_TX=explode("::date::",$content);


$Content = str_replace_once("::date::", $dispdate, $content);

$Content=str_replace_once("::time::", $hours, $Content);
print "<br><br>CONTENT = ".$Content."<br><br><br><br>";
////////////////////////////////////////////////////////////////////
but the last problem is i will have to merge hours[] and minutes[] array someting like below:

hours Array:
Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 )


minutes[] Array:
Array ( [0] => 30 [1] => 30 [2] => 30 [3] => 30 )

now i want an array which will merge hours[] and time[] and give me an time[] array someting like below:

time[] Array:
Array ( [0] => 11:30 [1] => 12:30 [2] => 13:30 [3] => 14:30 )

how can i do it...
this will be the last step... then i will have to just do the below and the entire string will be the way i want:
$Content=str_replace_once("::time::", $time, $Content);

now i am on the tail end. how will i merge hours[] and minutes[] and get time[] array

Thanks

Inigoesdr
11-13-2007, 07:02 AM
As long as the indexes match up you can do something like this:

$hours = array( 0 => 11, 1 => 12, 2 => 13, 3 => 14 );
$minutes = array( 0 => 30, 1 => 30, 2 => 30, 3 => 30 );
$time = array();

for($i = 0; $i < count($hours); $i++)
$time[$i] = $hours[$i] . ':' . $minutes[$i];
This outputs:

Array
(
[0] => 11:30
[1] => 12:30
[2] => 13:30
[3] => 14:30
)

bhakti_thakkar
11-13-2007, 07:27 AM
hi Inigoesdr,

thanks for your help. i was just lookin out a help on this. the indexex will always match up as i have DropDowns for both hours and minutes and default they will always have 00 and 00 in them.

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum