I've tried the following:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'austin') !== false) {
echo 'Austin metro';
} else if (strpos($url,'georgetown') !== false) {
echo 'Georgetown';
}
The issue is that this will match a URL that has austin anywhere.
For example, the url for georgetown parameter is like this: www.domain.com/austin/georgetown/
So if url has austin/georgetown, would like to display georgetown option.
Any suggestions? Thanks!
Go like this:
$url = 'www.domain.com/austin/georgetown'; //output: georgetown
if (strpos($url,'georgetown') !== false && strpos($url,'austin') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'georgetown') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'austin') !== false)
{
echo 'austin';
}
first if condition is checking if austin and georgetown, both are there in the url, if yes georgetown will be printed. rest two conditions are just checking for austin and georgetown individually.
See hear Php fiddle -> https://eval.in/715874
Hope this helps.
Related
I'm trying to get the title of a website that is entered by the user.
Text input: website link, entered by user is sent to the server via AJAX.
The user can input anything: an actual existing link, or just single word, or something weird like 'po392#*#8'
Here is a part of my PHP script:
// Make sure the url is on another host
if(substr($url, 0, 7) !== "http://" AND substr($url, 0, 8) !== "https://") {
$url = "http://".$url;
}
// Extra confirmation for security
if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
$urlIsValid = "1";
} else {
$urlIsValid = "0";
}
// Make sure there is a dot in the url
if (strpos($url, '.') !== false) {
$urlIsValid = "1";
} else {
$urlIsValid = "0";
}
// Retrieve title if no title is entered
if($title == "" AND $urlIsValid == "1") {
function get_http_response_code($theURL) {
$headers = get_headers($theURL);
if($headers) {
return substr($headers[0], 9, 3);
} else {
return 'error';
}
}
if(get_http_response_code($url) != "200") {
$urlIsValid = "0";
} else {
$file = file_get_contents($url);
$res = preg_match("/<title>(.*)<\/title>/siU", $file, $title_matches);
if($res === 1) {
$title = preg_replace('/\s+/', ' ', $title_matches[1]);
$title = trim($title);
$title = addslashes($title);
}
// If title is still empty, make title the url
if($title == "") {
$title = $url;
}
}
}
However, there are still errors occuring in this script.
It works perfectly if an existing url as 'https://www.youtube.com/watch?v=eB1HfI-nIRg' is entered and when a non-existing page is entered as 'https://www.youtube.com/watch?v=NON-EXISTING', but it doesn't work when the users enters something like 'twitter.com' (without http) or something like 'yikes'.
I tried literally everthing: cUrl, DomDocument...
The problem is that when an invalid link is entered, the ajax call never completes (it keeps loading), while it should $urlIsValid = "0" whenever an error occurs.
I hope someone can help you - it's appreciated.
Nathan
You have a relatively simple problem but your solution is too complex and also buggy.
These are the problems that I've identified with your code:
// Make sure the url is on another host
if(substr($url, 0, 7) !== "http://" AND substr($url, 0, 8) !== "https://") {
$url = "http://".$url;
}
You won't make sure that that possible url is on another host that way (it could be localhost). You should remove this code.
// Make sure there is a dot in the url
if (strpos($url, '.') !== false) {
$urlIsValid = "1";
} else {
$urlIsValid = "0";
}
This code overwrites the code above it, where you validate that the string is indeed a valid URL, so remove it.
The definition of the additional function get_http_response_code is pointless. You could use only file_get_contents to get the HTML of the remote page and check it against false to detect the error.
Also, from your code I conclude that, if the (external to context) variable $title is empty then you won't execute any external fetch so why not check it first?
To sum it up, your code should look something like this:
if('' === $title && filter_var($url, FILTER_VALIDATE_URL))
{
//# means we suppress warnings as we won't need them
//this could be done with error_reporting(0) or similar side-effect method
$html = getContentsFromUrl($url);
if(false !== $html && preg_match("/<title>(.*)<\/title>/siU", $file, $title_matches))
{
$title = preg_replace('/\s+/', ' ', $title_matches[1]);
$title = trim($title);
$title = addslashes($title);
}
// If title is still empty, make title the url
if($title == "") {
$title = $url;
}
}
function getContentsFromUrl($url)
{
//if not full/complete url
if(!preg_match('#^https?://#ims', $url))
{
$completeUrl = 'http://' . $url;
$result = #file_get_contents($completeUrl);
if(false !== $result)
{
return $result;
}
//we try with https://
$url = 'https://' . $url;
}
return #file_get_contents($url);
}
I'm trying to check whether the entered text is URL or string. My goal is i should get "URL Detected" if entered text is www.yout.com,http://stackoverflow.com, etc
if (stripos($text, ".com") !== false) {
echo "URL Detected";
}
you mast use filter_var
$incomingData = "www.google.com";
$url = filter_var($incomingData, FILTER_VALIDATE_URL);
if ($url !== false) {
echo "URL Detected";
}
Use regular expression to check if it's an url.
For example :
$regex = "/[-a-zA-Z0-9#:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9#:%_\+.~#?&//=]*)?/"
if(preg_match( $regex , "www.google.com" ){
echo "URL Detected"
}
I've tried the following:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
However if the url contains the word "care" or "car2" it will also trigger "Car Exists". How do I get a match if only "car" is found?
Change
if (strpos($url,'car') !== false) {
into
if (preg_match('/\bcar\b', $url) !== 0) {
Basically, you search for word car with no other alphanumeric surrounding it.
Hope this helps!
I am using below script to create sessions
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (
false !== strpos($url,'home') ||
false !== strpos($url,'display-job') ||
false !== strpos($url,'search-results-jobs') ||
false !== strpos($url,'find-jobs') ||
false !== strpos($url,'edit-profile1') ||
false !== strpos($url,'my-account/?myacount=1')
)
{
$_SESSION['page_name'] = 'jobseeker';
}
else {
$_SESSION['page_name'] = 'employer';
}
I can use the above script to check if someone is on one of the following sub pages but the problem is that i want to trigger a different session when someone is on the root of the webpage and I cant figure a way out.
try this
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$root_url = $_SERVER['SERVER_NAME'];
$arr_url = explode("/", $url);
unset($arr_url[sizeof($arr_url)-1]);
$new_url = implode("/",$arr_url);
if($new_url==$root_url)
{
// do your work
}
I am trying to figure out a condition that if there isn't the arguments "views" or "ckeditor" in the url, execute an echo on the page. The code that I am using and is not working is this:
<pre>
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
//if (!strpos($url,'views')) {
if ((!strpos($url,'views')) OR (!strpos($url,'ckeditor'))) {
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript">var $j = jQuery.noConflict();</script>' }
else {
echo '';
}
?>
</pre>
What is wrong in my script?
strpos() returns FALSE if the string is not found, and 0 if it is found at the beginning. In this case, you're trying to check if the URL contains either of these strings. So, you can simply check if it returns FALSE:
if ((strpos($url,'views') === FALSE) && (strpos($url,'ckeditor') === FALSE)) {
The above if condition will evaluate to TRUE if the URL doesn't contain views and ckeditor strings. If you only want to check for the existence of either one of the strings, then you can change && to ||.
a return value of 0 from strpos is a truthy response, however you are not checking for that. You should use strpos($url,'views') !== false
shouldn't this part have a semicolon
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript">var $j = jQuery.noConflict();</script>'
like this
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><script type="text/javascript">var $j = jQuery.noConflict();</script>';
only thing at first glance i noticed
If you are passing them thru a GET parameter you better use either $_GET or $_REQUEST instead of checking the url.
But if that's not the case, you may do something like:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strrpos($url, "views") === false || strrpos($url, "ckeditor") === false) {
...
}
Note for the 3 equal signs