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.....
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.....