简体   繁体   中英

Create string from regular expression PHP

I'm trying to create a string from a regular expression. I noticed that in Kohana framework's routing system you can set a route using something similar to a regular expression. Then you can create an url matching a route you've set. I'm trying to do something similar, but I can't find a clean way to do it. I've for example got the following regular expression:

/^(?<application>\w+)\/(?<controller>\w+)\/(?<method>\w+)\/(?<parameters>\w+)\/?$/

Now I want to be able to say that "application" equals "a", "controller" equals "b", "method" equals "c" and "parameters" equals "d". Then I should get a string that replaces the parts with the values specified. I can't find a good way to do this though. I've basically thought of two ways: 1) replace the corresponding values in the regular expression with the specified values, or 2) create a custom "regex syntax" that you can easily be used to create string and convert it to a "proper" regular expression when needed. Kohana uses the latter, but both ways sound quite bad to me. How would you do this?

Edit: I'll try to clarify it a bit. I for example pass the following string to the regular expression shown above using preg_match() : "myApplication/myController/myMethod/myParameters". This returns an array that has a couple of items, including 4 items with indexes "application", "controller", "method" and "parameters" with the corresponding values. Now I have to create the string "myApplication/myController/myMethod/myParameters" with the regular expression's pattern while I only have that array with the 4 items. How can I do this using PHP?

It should be pretty straightforward given that preg_match has support for named capturing groups (which your regular expression is, of course, using; more here ).

An example from PHP documentation :

<?php

$str = 'foobar: 2008';

preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);

/* This also works in PHP 5.2.2 (PCRE 7.0) and later, however 
 * the above form is recommended for backwards compatibility */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

print_r($matches);

?>

The above example will output:

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

So in your case, you can use your $matches array, eg $matches['application'] .


Edit: Okay, I did not fully understand the question.

An obvious problem with using regular expressions to generate strings is that a regular expression can match infinite strings. For example, /cat\s+rat/ matches all of:

cat rat
cat  rat
cat   rat

etc.

But in your example, nothing is undefined.

So your inclination to define a "safe" or singly-generatable language that is a subset of regular expressions is a good one, given a narrow use case. Then you could replace occurrences of /\(\?P?<(\w+)>\)/ with the value of the capturing group in there.

But since PHP doesn't really let you just use the value of the capturing group to access a variable during replacement in one step, you will likely have to do this in multiple steps… eg matching all occurrances of the above group, extracting the named groups, and then finally doing (in a loop over $matches as $match ) a simple string substitution from '(?P<'. $match. '>)' and '(?<'. $match. '>)' to the value of $$match (though in a safer way than that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM