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
    New Coder
    Join Date
    Aug 2004
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to get rid of the brackets [ ]

    I have a problem and cannot understand what I am doing wrong.. can anyone see something i have overlooked.

    I am parsing a template with php and replacing each case of [t_url] with a random url taken from a text file..
    In one mode the [ ] brackets are not being replaced.... yet they are in mode #1 its just that mode number 1 replaces all with same url so I was required to divert to mode #2 but it won't replace the brackets... makes no sense to me.


    Here is the code!
    *************
    // Replace [t_url]
    $turl = file("data/t_url.txt");
    for ($i=1; $i<=20; $i++) {
    $nr = rand(0, count($turl)-1);
    $t_url = $turl[$nr];
    #1) $str = str_replace("[t_url]", $t_url, $str);
    #2) $str = preg_replace("[t_url]", $t_url, $str, 1);
    }

    When i use #1 above... it swaps out the data compeltely, but of course it will place the same url in every swap... but it does get the whole thing.

    When I use #2 above.. it does a fantastic job of swaping the data based on a different url for every swap by using the limit to swap out one t_url at a time with a differnt url... problem is it leaves the [ ] brackets.. even though you can see I am searching for [t_url] it is swapping out the t_url and leaving the [ ] makes no sense to me... item #1 does not leave the brackets.. so why does item #2 leave them..

    Any ideas here.....

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    use [ php] and [ /php] to insert php code.

    try this:

    PHP Code:
    $str preg_replace("/\[t_url\]/"$t_url$str1); 
    is not tested.

    best regards

  • #3
    New Coder
    Join Date
    Aug 2004
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks that worked great.. still confused about why I did not have to escape it on the choice #1...

    So now that works... I have a new problem... for each template I have to use the limit of 1 so I can use a foreach to allow me to put in a new random t_url for each loop... problem is I don't know how many times to loop the template I am using has only 10 t_urls and other templates could have 20 or more... so as you can see my loop is set for 20 and that works as I guess it keeps checking and even if you loop 20 times and there is only 3 the three will get changed and it wastes time checking 17 more times..

    Question.. is anything like a blank here returned so I can test if and if blank or whatever stop; the loop sorta like loop until....

    Not sure if anything comes back to me when if fails to find one to replace... I hat using a big number for all as it has to be inefficent as hell.. besides not being too very professional....

    Thanks a lot for the super help above.. it is working great..

    Course if you know a simpler way to do what I am doing I would appreciate that also...

    Thanks
    Tom

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $oldstr $str;
    $str preg_replace("/\[t_url\]/"$t_url$str1);
    if(
    $oldstr == $str){
       break;

    not tested, not so good in my opinion, but get rid of some loops,

    PS: must be a better way.

    best regards


  •  

    Posting Permissions

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