使用正则表达式在查询字符串中查找标签

发布于 2024-08-26 04:57:00 字数 319 浏览 2 评论 0原文

我必须在我的 php 应用程序中设置一些路由规则,它们应该采用以下形式 /%var/something/else/%another_var

换句话说,我是一个正则表达式,它返回由 % 字符标记的每个 URI 片段,由 % 标记的字符串代表 var 名称,因此它们几乎可以是每个字符串。

另一个例子: 来自 /%lang/module/controller/action/%var_1 我希望正则表达式提取 lang 和 var_1

我尝试了类似的方法

/.*%(.*)[\/$]/

,但它不起作用......

I have to set some routing rules in my php application, and they should be in the form
/%var/something/else/%another_var

In other words i beed a regex that returns me every URI piece marked by the % character, String marked by % represent var names so they can be almost every string.

another example:
from /%lang/module/controller/action/%var_1
i want the regex to extract lang and var_1

i tried something like

/.*%(.*)[\/$]/

but it doesn't work.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤城病女 2024-09-02 04:57:00

鉴于它的路由规则,并且您可能在某个时候需要所有部分,您也可以按照经典方式拆分字符串:

$path_exploded = explode("/", $path);
foreach ($path_exploded as $fragment) if ($fragment[0] == "%") 
  echo "Found $fragment";

Seeing as it's routing rules, and you may need all the pieces at some point, you could also split the string the classical way:

$path_exploded = explode("/", $path);
foreach ($path_exploded as $fragment) if ($fragment[0] == "%") 
  echo "Found $fragment";
少年亿悲伤 2024-09-02 04:57:00
$str='/%var/something/else/%another_var';
$s = explode("/",$str);
$whatiwant = preg_grep("/^%/",$s);
print_r($whatiwant);
$str='/%var/something/else/%another_var';
$s = explode("/",$str);
$whatiwant = preg_grep("/^%/",$s);
print_r($whatiwant);
掀纱窥君容 2024-09-02 04:57:00

我不认为需要使用正则表达式来减慢脚本的速度……trim()和explode()可以完成您需要的一切:

function extract_url_vars($url)
{
    if ( FALSE === strpos($url, '%') )
    {
        return $url;
    }

    $found = array();
    $parts = explode('/%', trim($url, '/') );

    foreach ( $parts as $part )
    {
        $tmp     = explode('/', $part);
        $found[] = ltrim( array_shift($tmp), '%');
    }

    return $found;
}

// Test
    print_r( extract_url_vars('/%lang/module/controller/action/%var_1') );

// Result:
Array
(
    [0] => lang
    [1] => var_1
)

I don’t see the need to slow down your script with a regex … trim() and explode() do everything you need:

function extract_url_vars($url)
{
    if ( FALSE === strpos($url, '%') )
    {
        return $url;
    }

    $found = array();
    $parts = explode('/%', trim($url, '/') );

    foreach ( $parts as $part )
    {
        $tmp     = explode('/', $part);
        $found[] = ltrim( array_shift($tmp), '%');
    }

    return $found;
}

// Test
    print_r( extract_url_vars('/%lang/module/controller/action/%var_1') );

// Result:
Array
(
    [0] => lang
    [1] => var_1
)
送君千里 2024-09-02 04:57:00

您可以使用:

$str = '/%lang/module/controller/action/%var_1';    
if(preg_match('@/%(.*?)/[^%]*%(.*?)$@',$str,$matches)) {
        echo "$matches[1] $matches[2]\n"; // prints lang var_1    
}

You can use:

$str = '/%lang/module/controller/action/%var_1';    
if(preg_match('@/%(.*?)/[^%]*%(.*?)$@',$str,$matches)) {
        echo "$matches[1] $matches[2]\n"; // prints lang var_1    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文