简体   繁体   中英

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.

$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

Look, the GET data is URL ENCODED. You need use string urldecode ( string $str ) to decode and 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 MANUAL - 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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