Search Text from Current Page URL – Search from URL PHP Script

by Hiroshi on February 18, 2012

in Checks, URL

Sometimes we need to get something from current URL or page or we need to check for something (text or variable) in URL which can determine further action in that page. Suppose you want to match any word in the current URL and if that word or text is present in URL, you want to change or add any meta keyword in the page then continue reading. First we will get the current URL of page then we will match (find, search) any word from that URL.

If you want to search anything or match anything from current URL then this script is for you.

Getting Current Page URL in PHP

<?php
// page url complete
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
//  echo curPageURL();
// $pgu contains complete page url, call this var in any page
 $pgu=curPageURL();
?>

Match or Find Specific Text from Current URL in PHP

<?php
// The i after the pattern delimiter indicates a case-insensitive search
if (preg_match("/place text to match here/i", "$pgu")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

Related Posts

Previous post:

Next post: