...

View Full Version : How to get rid of the brackets [ ]



tommytx
03-13-2008, 06:44 AM
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.....

oesxyl
03-13-2008, 06:57 AM
use [ php] and [ /php] to insert php code.

try this:



$str = preg_replace("/\[t_url\]/", $t_url, $str, 1);


is not tested.

best regards

tommytx
03-13-2008, 07:27 AM
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

oesxyl
03-13-2008, 07:37 AM
$oldstr = $str;
$str = preg_replace("/\[t_url\]/", $t_url, $str, 1);
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum