Thanks for sharing your code! The key question is:
Why does your first version with run_highlight() not work as expected, but the second version (inline code) works?
Quick summary of difference
First version (not working):
function run_highlight($title, $content, $link, $patterns, $client) {
$content = search_ticker($link, $content);
$highlighted = highlight([
'client' => $client,
'where_search' => ['title' => $title, 'content' => $content],
'patterns' => $patterns
]);
$highlighted['content'] = add_title_to_content($title, $link, $highlighted['content']);
return $highlighted;
}
foreach ($data["content"] as $index=>$value) {
$title = $value['title'];
$link = $value['link'];
$content = $value['content'];
$client = 'reader';
$highlighted = run_highlight($title, $content, $link, $patterns, $client);
[$title, $content] = array_values($highlighted);
$data["content"][$index]['content'] = $content;
}
Second version (working):
foreach ($data["content"] as $index=>$value) {
$title = $value['title'];
$link = $value['link'];
$content = $value['content'];
$client = 'reader';
$content = search_ticker($link, $content);
$highlighted = highlight([
'client' => $client,
'where_search' => ['title' => $title, 'content' => $content],
'patterns' => $patterns
]);
[$title, $content] = array_values($highlighted);
$content = add_title_to_content($title, $link, $content);
$data["content"][$index]['content'] = $content;
}
What could be the reason?
Let's carefully compare and analyze the difference.
1. Order of add_title_to_content call
-
In the first (not working), you call
$highlighted['content'] = add_title_to_content($title, $link, $highlighted['content']);
inside run_highlight(), before returning $highlighted.
-
In the second (working), you do:
$content = add_title_to_content($title, $link, $content);
after unpacking $highlighted.
Possible issue: in the first version, $title is the original title passed to the function, but inside highlighted, both title and content might have changed, but you are applying add_title_to_content with the old $title.
If highlight() modifies $title, then applying add_title_to_content with the old title is incorrect.
2. array_values($highlighted) unpacking
In both cases you have:
[$title, $content] = array_values($highlighted);
But the array returned by highlight() is presumably an associative array with keys something like 'title' and 'content'.
array_values will return the array values in insertion order, which may not be stable or consistent.
If highlight() returns e.g.:
[
'title' => 'new title',
'content' => 'new content'
]
array_values($highlighted) will be: ['new title', 'new content'], so:
[$title, $content] = array_values($highlighted);
will assign correctly.
But, if you modify $highlighted['content'] after that (or before), this can cause confusion.
3. Inconsistent modification of $title and $content
In your first version:
$highlighted['content'] = add_title_to_content($title, $link, $highlighted['content']);
return $highlighted;
You modify 'content' inside the returned array before returning.
Then in the caller, you do:
[$title, $content] = array_values($highlighted);
So $content already has add_title_to_content applied.
In the second version:
[$title, $content] = array_values($highlighted);
$content = add_title_to_content($title, $link, $content);
So you first extract $title and $content from highlight, then apply add_title_to_content.
So why does the first not work?
My guess:
The $title used in add_title_to_content() inside run_highlight() is the original title, not the highlighted title.
If highlight() modifies $title, your first version applies the content modification with the wrong $title.
Whereas your second version always uses the updated $title returned from highlight().
Proposed fix for run_highlight():
Apply add_title_to_content() after extracting both $title and $content:
function run_highlight($title, $content, $link, $patterns, $client) {
$content = search_ticker($link, $content);
$highlighted = highlight([
'client' => $client,
'where_search' => ['title' => $title, 'content' => $content],
'patterns' => $patterns
]);
// extract highlighted values
[$title, $content] = array_values($highlighted);
// now apply add_title_to_content with updated title
$content = add_title_to_content($title, $link, $content);
return ['title' => $title, 'content' => $content]; // return associative array for clarity
}
foreach ($data["content"] as $index=>$value) {
$title = $value['title'];
$link = $value['link'];
$content = $value['content'];
$client = 'reader';
$highlighted = run_highlight($title, $content, $link, $patterns, $client);
$data["content"][$index]['content'] = $highlighted['content'];
$data["content"][$index]['title'] = $highlighted['title'];
}
Why does this help?
- You use the updated
$title returned from highlight().
- You apply
add_title_to_content() with the correct title.
- You avoid modifying
$highlighted['content'] inside run_highlight() in a way inconsistent with the title.
Other notes:
- Be careful with unpacking arrays — returning associative array and using keys (
['title' => ..., 'content' => ...]) is clearer than relying on array_values() and positional order.
- It’s safer and easier to maintain.
If you want, I can help you rewrite the functions for clarity and robustness!