Validate whether a variable's contents is a domain [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to validate domain name in PHP?
Better way to validate a URL in PHP
How do I check, if $variable is a site address?
Like, for this it should give true:
$varialbe = 'http://google.com';
For this false:
$variable = 'this value can be anything, but we know its not a domain';

Use the filter_var function (also see types of filters) provided by PHP:
$is_url = filter_var($url, FILTER_VALIDATE_URL);

Related

How do I extract username and domain from an email using PHP script? [duplicate]

This question already has answers here:
Is there a more efficient way to get email suffix than explode? (PHP)
(5 answers)
Closed 3 years ago.
I'm trying to code a PHP script that is given a string with an email address that extracts the username and domain.
For example: username#domain.com -> username | domain.com
I want to take off the "#".
I've tried using trim() but that only works at the start and at the end of a string.
<?php
$email = 'username#domain.com';
$getArray = explode("#",$email);
echo "<pre>";
print_r($getArray);
echo $getArray[0].'<br/>';
echo $getArray[1];
?>

how to check if string has server ih php [duplicate]

This question already has answers here:
Validating a URL in PHP [duplicate]
(3 answers)
Closed 6 years ago.
How to check if string has a server depending if url is absolute or relative?
function hasServer($url){
...
}
If this, returns true
hasServer('http://www.google.com/');
else return false
hasServer('/about-us/team/');
I think this is what you're looking for:
$url = 'http://www.google.com/';
$hasServer = filter_var($url, FILTER_VALIDATE_URL);
There are actually a few options from my experience. One would be to use the headers() function and then to analyse what information you obtained inside the resulting array.
$arrayResult = headers('http://www.google.com/');
foreach ($arrayResult as $value)
{
echo "-- ".$value."<br>";
}
The output should give you all the information you need regarding if the url actually exists.
A simpler solution in my opinion is to just check if the fopen() function actually works on the url!
if (fopen('http://www.google.com/', "r")
{
echo "the URL exists!<br>";
}
You can choose which one servers your needs better.

Check if a string is a part of another string (PHP) [duplicate]

This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 7 years ago.
I'd like to check if a string is equal to another string. By equal, I mean that I want to check if the second string includes the first one in order to make mysql join's in a dynamic way. (I hope I'm clear, sorry for my english...)
I've seen some functions as strcmp() but it only checks if it is purely equal.
It's the same as "$var1 === $var2".
Is there a function which can do that ? Or could you give me some leads to do that ?
if (strpos($a,'are') !== false) {
echo 'true';
}
How do I check if a string contains a specific word in PHP?

How to extract last part in PHP? [duplicate]

This question already has answers here:
How do I extract query parameters from a URL string in PHP?
(3 answers)
Closed 8 years ago.
This would be an example:
redirect
dynamic_word can be changed because it is dynamic. When click "redirect", dynamic_word will be extracted. So, how to extract it in redirect.php file ? Thanks !
Use $_GET to get parameters from an URL
<?php
$thatName = $_GET['q'];
echo $thatName;
Result
dynamic_word
If samitha's correct looking answer is incorrect then perhaps you mean you would like to extract the dynamic word from a string.
In that case you could do
<?php
$string = 'http://mywebsite.com/redirect.php&q=dynamic_word';
$ex_stirng = explode('&q=', $string);
$dynamic_word = $ex_string(1);
?>
Or even use the strstr function:
http://www.php.net/manual/en/function.strstr.php

PHP built in function to determine if email is valid [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to validate an Email in PHP?
Does PHP have a built in function for determining if an email address is formatted properly. I know it can't go out and check if the email is actually active; I'm talking about just confirming that an email address is structurally correct.
filter_var can do this:
$result = filter_var( 'bob#example.com', FILTER_VALIDATE_EMAIL );
It returns false when it failed validation, and returns the e-mail address otherwise.

Categories