Skip to content

Error: Array to string conversion in mf2 #482

@edent

Description

@edent

The function compare_urls() uses array_intersect() :

public function compare_urls( $needle, $haystack, $schemeless = true ) {
if ( ! $this->is_url( $needle ) ) {
return false;
}
if ( is_array( reset( $haystack ) ) ) {
return false;
}
if ( true === $schemeless ) {
// Remove url-scheme.
$schemeless_target = preg_replace( '/^https?:\/\//i', '', $needle );
// Add both urls to the needle.
$needle = array( 'http://' . $schemeless_target, 'https://' . $schemeless_target );
} else {
// Make $needle an array.
$needle = array( $needle );
}
// Compare both arrays.
return array_intersect( $needle, $haystack );
}

The intersect requires both variables to be one-dimensional arrays. But, sometimes, this isn't the case.

I received a $needle of:

array (
  0 => 'http://shkspr.mobi/blog/2024/07/llms-are-good-for-coding-because-your-documentation-is-shit/',
  1 => 'https://shkspr.mobi/blog/2024/07/llms-are-good-for-coding-because-your-documentation-is-shit/',
)

But the $haystack was:

array (
  0 => 'githubcopilot',
  1 => 'https://brid.gy/comment/mastodon/@edent@mastodon.social/112773307021860322/githubcopilot',
  2 => 
  array (
    'type' => 
    array (
      0 => 'h-card',
    ),
    'properties' => 
    array (
      'uid' => 
      array (
        0 => 'tag:mastodon.social,2013:7112',
      ),
      'name' => 
      array (
        0 => 'Edent',
      ),
      'url' => 
      array (
        0 => 'https://mastodon.social/@Edent',
      ),
    ),
    'value' => 'https://mastodon.social/@Edent',
  ),
)

Because the 2nd element is an array, an error is thrown Array to string conversion.

Is there a way to ensure the $haystack is a simple array?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions