I have a loop, which takes a large amount of text in each iteration and replaces specific placeholder ('token') with some other content like so:
$string = $pageContent;
foreach($categories as $row) {
$images = $mdlGallery->getByCategory($row['id']);
if (!empty($images)) {
$plug = Plugin::get('includes/gallery', array('rows' => $images));
$string = str_replace($row['token'], $plug, $string);
}
}
The Plugin class and it's get() method simply takes the right file from a specific directory and outputs buffer as a string.
There might be a large number of categories therefore I wonder whether whether it would be better to first check the input string for an occurrence of the specific 'token' before going through populating all images from a given category using strpos() function like so:
foreach($categories as $row) {
if (strpos($string, $row['token']) !== false) {
$images = $mdlGallery->getByCategory($row['id']);
if (!empty($images)) {
$plug = Plugin::get('includes/gallery', array('rows' => $images));
$string = str_replace($row['token'], $plug, $string);
}
}
}
My concern is the performance - would this help? - consider $string to potentially contain a large number of characters (TEXT field type in MySQL)?
foreachconnect to the DB? Or read entire files into memory (file_get_contents)?$mdlGallery->getByCategory($row['id']);a database call?