Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:12:47+00:00 2026-05-17T00:12:47+00:00

Here is the match_url function I am using at the moment function match_url($urls,$url) {

  • 0

Here is the match_url function I am using at the moment

function match_url($urls,$url) {
    foreach($urls as $regex=>$args) {
        $r = str_replace('/','\/',$regex);
        preg_match("/$r/i",$url,$matches);
        if(count($matches) > 0) {
            $dispatch = array();
            $args['args'] = array();
            if(empty($args) && !empty($matches['controller'])) {
                $dispatch['controller'] = $matches['controller'];
                $dispatch['action'] = $matches['action'];
            } else {
                foreach($matches as $k=>$v) {
                    if(is_string($k)) {
                        $args['args'][$k] = $v;
                    }
                }
                $dispatch = $args;
            }

            return $dispatch;
        }
    }
}

Here are some example $urls

$urls = array(
    'articles/(?P<year>\d{4})' => array('controller'=>'articles','action'=>'show','name'=>'article_view'),
    'admin/(?P<controller>[-\w]+)/(?P<action>[-\w]+)' => array(),
    '(?P<controller>[-\w]+)/(?P<action>[-\w]+)' => array()
);

the following url articles/2010 would return an array like:

Array
(
[controller] => articles
[action] => show
[name] => article_view
[args] => Array
(
[year] => 2010
)

)

This works as expected but now I am wanting to create a function that will allow me to build a url from given url name and args. For example:

$name = "article_view";
$args = array("year"=>2010);
echo url_for($name,$args);
//should output articles/2010

The part that I am having trouble with is figuring out how I can replace the variables from the url regex with the passed args.

Is there a simple method of doing this?

Any suggestions or pointers in the right direction would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:12:48+00:00Added an answer on May 17, 2026 at 12:12 am

    Here is the solution I came up with:

    function url_for($urls,$name,$args) {
        $url = "";
        foreach($urls as $k=>$v) {
            if(!empty($v['name']) && $v['name'] == $name) {
                $url = $k;
                foreach($args as $key=>$value) {
                    $url = preg_replace("/\(\?P\<$key\>(.*)\)/",$value,$url);
                }
            }
        }
        return $url;
    }
    

    Which now works as I expected

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my regex code: preg_match_all('/background[-image]*:[\s]*url\([|\']+(.*)[|\']+\)/', $css, $matches, PREG_SET_ORDER); It looks for CSS that looks
How can eregi() function can be replaced here with preg_match() function in below given
I am using jQuery autocomplete from here : http://www.pengoworks.com/workshop/jquery/autocomplete.htm $(#TestTextbox).autocomplete( '<%= Url.Action(LookupAction) %>', {
I am using a PHP function to automatically turn URLs in a text string
So, here is the function for pre-filtering CHILD: function(match){ if ( match[1] === nth
If i type www.google.com or http://www.google.com using auto_link() function will add link correctly.. However,
I'm using http://www.barattalo.it/2010/08/09/send-push-notification-to-iphone-with-php-and-pushme-to/ When i call the function i get the error messages. Notice:
Ok, so here's the skinny... I've worked out a jQuery function that will first
I am having trouble using the XSLT 1.0 function library (since .NET/Visual Studio doesn't
I am trying to use RegEX to match a URL pattern. I found an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.