簡體   English   中英

正則表達式不在PHP中的括號之間打印字符串

[英]Regex Not printing string between parenthesis in PHP

我已經對此進行了研究,無法弄清楚我在做什么錯。 我有以下從$ _GET獲取的變量。

$monitorname = $_GET['monitorname'];

該網址應類似於: 985_Sands-Road(54dd14d80bd5f777184cb9c8) ,它在URL中的實際外觀為:

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;

提前非常感謝

看,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手冊-URLDECODE
PHP手冊-HTML_ENTITY_DECODE

更新!

$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;

我的第一個想法也是urldecode(),但在這種情況下不起作用...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM