hello wordpress/ url/ php experts I really appreciate your help in advance.
I set up a wordpress website and the url for example
www.domain.com/hello-world-how-are-you
so what i want to do is within single.php script, i want to parse the url and explode the url by "-" and take "hello" and "you" as a php variable.
i don't have too much knowledge on the server configuration, it would be great if this can be done using php only.
once again, thank you in advance.
To grab the "page name"
<?php
// gets you the path after http://www.yourdomain.com
$path = $_SERVER["REQUEST_URI"];
$current_url = explode("-", $path);
$current_url = $current_url[1];
echo "path ".$path . "<br />" ;
echo "exploded url " .$current_url;
?>
I was able to make this method work with preg_replace to grab arrays of each word (stripping out everything but numbers and letters in a string.
<?php
// gets /beach/coast-guard-beach/
$path = $_SERVER["REQUEST_URI"];
$preg = preg_split('/[^a-z0-9]/', $path);
foreach ($preg as $key) {
print $key. "<br /> ";
}
?>
prints:
beach coast guard beach
In short:
$url = 'hello-world-how-are-you'; // chop off the domain part first
$parts = explode('-', $url);
$var1 = $parts[0];
$var2 = end($parts);
The URL needs parsed first, check into the SERVER array (SERVER['REQUEST_URI']) and see what fits your needs. (via error_log(print_r($_SERVER,1))
)