Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2007, 05:45 AM   PM User | #1
bhakti_thakkar
Regular Coder

 
Join Date: Sep 2006
Location: India Mumbai
Posts: 248
Thanks: 13
Thanked 1 Time in 1 Post
bhakti_thakkar is an unknown quantity at this point
help in search and replace

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

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

Last edited by bhakti_thakkar; 11-13-2007 at 05:48 AM..
bhakti_thakkar is offline   Reply With Quote
Old 11-13-2007, 06:27 AM   PM User | #2
bhakti_thakkar
Regular Coder

 
Join Date: Sep 2006
Location: India Mumbai
Posts: 248
Thanks: 13
Thanked 1 Time in 1 Post
bhakti_thakkar is an unknown quantity at this point
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
bhakti_thakkar is offline   Reply With Quote
Old 11-13-2007, 07:02 AM   PM User | #3
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
As long as the indexes match up you can do something like this:
PHP Code:
$hours = array( => 11=> 12=> 13=> 14 );
$minutes = array( => 30=> 30=> 30=> 30 );
$time = array();

for(
$i 0$i count($hours); $i++)
    
$time[$i] = $hours[$i] . ':' $minutes[$i]; 
This outputs:
Code:
Array
(
    [0] => 11:30
    [1] => 12:30
    [2] => 13:30
    [3] => 14:30
)
Inigoesdr is offline   Reply With Quote
Users who have thanked Inigoesdr for this post:
bhakti_thakkar (11-13-2007)
Old 11-13-2007, 07:27 AM   PM User | #4
bhakti_thakkar
Regular Coder

 
Join Date: Sep 2006
Location: India Mumbai
Posts: 248
Thanks: 13
Thanked 1 Time in 1 Post
bhakti_thakkar is an unknown quantity at this point
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.

bhakti_thakkar is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:12 AM.


Advertisement
Log in to turn off these ads.