douwen8424 2015-04-07 22:55
浏览 29
已采纳

正则表达式不在PHP中的括号之间打印字符串

I have researched this up and down am can't figure out what I'm doing wrong. I have the following variable which I get from $_GET.

$monitorname = $_GET['monitorname'];

which should look something similar to: 985_Sands-Road(54dd14d80bd5f777184cb9c8), in the URL it actually looks like:

index.php?monitorname=985_Sands-Road%26%2340%3B54dd14d80bd5f777184cb9c8%26%2341%3B&alertType=0"

I'm trying to use Regex to get the text between the parentheses, here is my regex code:

preg_match('/\(([^)]+)\)/', $monitorname, $match);
$id = $match[1];
echo $id;

However no matter what I do, it won't echo the ID. What can I look at and fix in this? It must be noted that when I save the string explicitly as a variable it works fine:

$text = '985_Sands-Road(54dd14d80bd5f777184cb9c8)';
preg_match('/\(([^)]+)\)/', $text, $match);
$id = $match[1];
echo $id;

Thanks so much in advance

  • 写回答

2条回答 默认 最新

  • douchuntang2827 2015-04-07 23:15
    关注
    $monitorname = $_GET['monitorname'];
    $src=array('(',')');
    $rep=array('(',')');
    $monitorname=str_replace($src,$rep,$monitorname);
    
    //now your code
    
    preg_match('/\(([^)]+)\)/', $monitorname, $match);
    $id = $match[1];
    echo $id;
    

    My first thought was urldecode() too, but it doesn't work in this case...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵