PHP code example of gmazzap / url-to-query

1. Go to this page and download the library: Download gmazzap/url-to-query library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gmazzap / url-to-query example snippets

 php
$args = url_to_query( '/sample-page' );
// $args = array(  'pagename' => 'sample-page' );
 php
$args = url_to_query( '/?attachment_id=880' );
$args = array(  'attachment_id' => '880' );
 php
$args = url_to_query( '/', array( 'attachment_id' => '880' ) );
// $args = array(  'attachment_id' => '880' );
 php
$args = url_to_query( '/', array( 'attachment_id' => '880', 'foo' => 'bar' ) );
// $args = array(  'attachment_id' => '880' );
 php
add_action( 'init', 'my_rew_rules' );

function my_rew_rules() {
  add_rewrite_tag('%food%', '([^&]+)');
  add_rewrite_tag('%variety%', '([^&]+)');
  add_rewrite_rule(
    '^nutrition/([^/]*)/([^/]*)/?',
    'index.php?page_id=12&food=$matches[1]&variety=$matches[2]',
    'top'
  );
}

add_action( 'footer' function() {
  $args = url_to_query( '/nutrition/cake/cherry/' )
  // $args = array( 'page_id' => '12', 'food' => 'cake', 'variety' => 'cherry' );
} );
 php
$resolver = new GM\UrlToQuery();
$args = $resolver->resolve( 'http://example.com/sample-page', array( 'page' => '2' ) );
// $args = array( 'pagename' => 'sample-page', 'page' => '2' );
 php
$resolver = new GM\UrlToQuery();

$args1 = $resolver->resolve( 'http://example.com/sample-page', array( 'page' => '2' ) );
// $args1 = array( 'pagename' => 'sample-page', 'page' => '2' );

$args2 = $resolver->resolve( '/?attachment_id=880' );
// $args2 = array( 'attachment_id' => '880' );

$args3 = $resolver->resolve( 'http://example.com/category/uncategorized/' );
// $args3 = array( 'category_name' => 'uncategorized' );