Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    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.

  • #2
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    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

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    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
    )

  • Users who have thanked Inigoesdr for this post:

    bhakti_thakkar (11-13-2007)

  • #4
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post
    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.



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •