2015-04-07 126 views
0

我已经研究了这个上下我无法弄清楚我做错了什么。我有$ _GET获得的以下变量。正则表达式不在PHP中的括号之间打印字符串

$monitorname = $_GET['monitorname']; 

看起来应该类似于:985_Sands-Road(54dd14d80bd5f777184cb9c8),在它实际上看起来像网址:

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

我试图用正则表达式来获得括号之间的文本,这里是我的正则表达式代码:

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

但无论我做什么,它都不会回显ID。我能看到什么并在此修复?必须指出的是,当我明确地保存字符串作为一个变量,它工作正常:

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

感谢这么多提前

+0

你有没有尝试使用'的preg_match('/%26%2340%3B(。*? )%26%2341%3B /',$ text,$ match);'?我认为这些就像是'<'和'>'。 –

+0

@stribizhev感谢您的评论。我只是试了一下,仍然没有。 – Moe

回答

0
$monitorname = $_GET['monitorname']; 
$src=array('&#40;','&#41;'); 
$rep=array('(',')'); 
$monitorname=str_replace($src,$rep,$monitorname); 

//now your code 

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

我首先想到的是urldecode()也一样,但它并没有在这种情况下工作...

+0

OMG这真棒...它的工作。你无法想象我花了多长时间来解决这个问题。我现在想给你一个大的拥抱!非常感谢。 – Moe

+0

Np,很高兴帮助,echo $ monitorname;是有帮助的。 :) – sinisake

+1

而现在,我可以看到 - Jefersonfs找到了更好的解决方案。 :) – sinisake

2

看,GET数据是URL编码。 您需要使用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"));` 

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

的更多信息: PHP MANUAL - URLDECODE
PHP MANUAL - HTML_ENTITY_DECODE

更新!

+0

我的意思是把这个添加到笔记中,我试过解码然后回显结果,它只是空白。当我编码时,我一定要回显编码,我可以看到它,只是正则表达式不起作用。我完全被这个困惑了。 – Moe

相关问题