简体   繁体   English

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

[英]Regex Not printing string between parenthesis in 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. 我有以下从$ _GET获取的变量。

$monitorname = $_GET['monitorname'];

which should look something similar to: 985_Sands-Road(54dd14d80bd5f777184cb9c8) , in the URL it actually looks like: 该网址应类似于: 985_Sands-Road(54dd14d80bd5f777184cb9c8) ,它在URL中的实际外观为:

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. 但是,无论我做什么,它都不会回显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 提前非常感谢

Look, the GET data is URL ENCODED. 看,GET数据是URL编码的。 You need use string urldecode ( string $str ) to decode and string html_entity_decode ( string $string [, int $quote_style [, string $charset ]] ) . 您需要使用string urldecode ( string $str )进行解码和string html_entity_decode ( string $string [, int $quote_style [, string $charset ]] )

`html_entity_decode(urldecode("985_Sands-Road%26%2340%3B54dd14d80bd5f777184cb9c8%26%2341%3B"));`

Like : 喜欢 :
$monitorname = $_GET["monitorname"]; $monitorname = html_entity_decode(urldecode($monitorname)); //CODE preg_match('/\\(([^)]+)\\)/', $monitorname, $match); $id = $match[1]; echo $id;

More information in : PHP MANUAL - URLDECODE 有关更多信息: PHP手册-URLDECODE
PHP MANUAL - HTML_ENTITY_DECODE PHP手册-HTML_ENTITY_DECODE

Updated! 更新!

$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... 我的第一个想法也是urldecode(),但在这种情况下不起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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