Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从大括号中获取url参数_Php_String_Url_Strpos - Fatal编程技术网

Php 从大括号中获取url参数

Php 从大括号中获取url参数,php,string,url,strpos,Php,String,Url,Strpos,我有这个网址:。/foo::bar{2}233{13}171{1}1{20}0.html 参数在{}中,值在后面 有了这个,我可以得到一个参数——尽管有花括号: if (false !== strpos($url,'{2}')) { echo 'success'; } else { echo 'error'; } 我想获得{}后面的每个值,请尝试使用 $str='../foo::bar{2}233{13}171{1}1{20}0.html'; $patt

我有这个网址:
。/foo::bar{2}233{13}171{1}1{20}0.html

参数在
{}
中,值在后面

有了这个,我可以得到一个参数——尽管有花括号:

if (false !== strpos($url,'{2}')) {
        echo 'success';
    } else {
        echo 'error';
}
我想获得
{}

后面的每个值,请尝试使用

$str='../foo::bar{2}233{13}171{1}1{20}0.html';
$pattern=“/\{\d+\}/”;
preg_match_all($pattern,$str,$matches);
$prevStart=0;
foreach($matches[0]为$match)
{
$matchLen=strlen($match);
$matchPos=strpos($str,$match,$prevStart);
//试着找到下一个打开的花括号的位置
$openCurlyPos=strpos($str,'{',$matchPos+$matchLen);
//如果没有找到(.html下一步显示),则搜索
if($openCurlyPos==false)
$openCurlyPos=strpos($str,'.',$matchPos+$matchLen);
$length=$openCurlyPos-($matchPos+$matchLen);
$prevStart=$openCurlyPos;
echo$match.“:”.substr($str,$matchPos+$matchLen,$length)。“
”; } /* 结果: {2}: 233 {13}: 171 {1}: 1 {20}: 0 */

我知道这种方法可能是多余的,但我不知道如何使用正则表达式来实现。对于人们来说,这似乎更容易理解。

使用
preg\u match\u all
您可以提取键和值,下面是一个示例模式

$matches = null;
$returnValue = preg_match_all('/\\{(\\d+)\\}(\\d+)\\b/', '../foo::bar{2}233{13}171{1}1{20}0.html', $matches, PREG_SET_ORDER);
如果我们忽略双转义,它的作用如下

  • 找到一个{
  • 1个或多个数字,直到a}
  • 1个或多个数字,直到出现单词边界
    • 试试这个

      $pieces = explode("}", $str);
      
      获取所有奇数索引元素

      $pieces[1],$pieces[3],$pieces[5]
      

      等等。

      键和值总是数字的吗?是的,它们是。谢谢。谢谢!现在我得到了所有的参数。我要添加什么才能得到它们的值?@user1957345没问题。还有一个问题:我怎样才能去掉回显的花括号?必须删除这部分:
      $match.:”。
      谢谢,但我希望参数在没有花括号的情况下被回显-例如,
      2:233
      而不是
      {2}:233
      $pieces[1],$pieces[3],$pieces[5]