Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
PHP Check URL parameters against an array

I'm using the following code to echo a part of a URL and my dynamic URL now looks like this.

https://example.test/test.php?name=living-room

But the condition is that, it will only echo if the name part of the URL is in my array.

$array = array('kitchen', 'bedroom', 'living room', 'dining room');
if (in_array($_GET['name'], $array))
{echo $_GET['name'];}  
else {header("HTTP/1.0 404 Not Found");}

What I'm trying to do is treat the - in URL's name part as spaces.

For example, living-room should be equal to living room in my array and it should echo the value in my array (living room) instead of (living-room).

  1. So if the URL's value is living-room, we check the array and since living room exists in the array living room will get echoed.
  2. In the same way as before, if the URL's value is dining-room, since dining room exists in my array, dining room will get echoed.

I'm having a hard time finding the correct logic to this.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can do a string replace while echoing the string from URL parameter. You can do something like this:

header("Content-Type: text/plain");
$name = $_GET['name'];
$name = str_replace("-"," ",$name); // Replace - with space
    
$array = ['kitchen', 'bedroom', 'living room', 'dining room'];
    
if (in_array($name, $array, true)) {
    // Found
    echo $name, "\n"; 
} else {
    // Not found
    header("HTTP/1.0 404 Not Found");
}
over 4 years ago · Santiago Trujillo Report

0

You can use str_replace to replace "-" (U+002D HYPHEN-MINUS) with " " (U+0020 SPACE) in your string like so:

header("Content-Type: text/plain");

$array = ['kitchen', 'bedroom', 'living room', 'dining room'];
$needle = str_replace("-", " ", $_GET['name']);
if (in_array($needle, $array, true)) {
    echo $needle, "\n";
} else { 
    header("HTTP/1.0 404 Not Found");
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!