Skip to content

refactor: use preg_replace_callback() in auto_link() - #10844

Open
paulbalandan wants to merge 1 commit into
codeigniter4:developfrom
paulbalandan:auto-link-callback
Open

paulbalandan wants to merge 1 commit into
codeigniter4:developfrom
paulbalandan:auto-link-callback

Conversation

@paulbalandan

Copy link
Copy Markdown
Member

Description
auto_link() collected matches with preg_match_all() and PREG_OFFSET_CAPTURE, then spliced the replacements back with substr_replace() in reverse order so the offsets stayed valid. Intelephense reports Expected type 'array|int'. Found 'string' on both substr_replace() offsets, because it types each offset capture as a string, an int or an array.

This replaces both loops with preg_replace_callback(), which needs no offsets or reverse iteration. The regexes are unchanged. The URL callback builds the anchor with sprintf(), and the email callback returns safe_mailto() for a valid address and the matched text otherwise.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the refactor Pull requests that refactor code label Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Pull requests that refactor code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants