PHP url preg_match - php

I have a problem with this preg_match
function isValidURL($url){
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
if(!isValidURL($url)){
echo 'false';
} else
echo 'true';
For these links should display - true
/test.html
/testowa-strona_9067.html
/567890.html?get=test
/costam.html?get=2&f[]=k&f[]=k2
And for those false
/.html
/ąęśćzmn-ż.html
/testmhtml
/%67%68%89(i&.html?get=34
But it always displays true

You should try this :
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Not a valid URL');
}

Related

Loop until the request response message equals to "true" in php

I'm trying to make a php script that would make a loop that would get the contents of my site/server and if the text response is for example "false" then it would do the same thing, basically will loop until the site's text response will echo "true".
This is what i tried:
$getcontents = file_get_contents("http://example.com/script.php"); // it will echo false
if (strpos($getcontents , 'false')) {
$getcontents = file_get_contents("http://example.com/script.php");
else if (strpos($getcontents , 'false')) {
$getcontents = file_get_contents("http://example.com/script.php");
}
else if (strpos($getcontents , 'true')) {
echo "finished".;
}
I'm not sure if this is the right way or even if it is possible and i apologize in advance if i did not explain myself very well. Thank you for attention!
You could use a regular while loop.
$getcontents = 'false'; //set value to allow loop to start
while(strpos($getcontents , 'false') !== false) {
$getcontents = file_get_contents("http://example.com/script.php");
}
echo "finished";
This will loop until $getcontents does not contain false.
You could also use a recursive function like this.
function check_for_false() {
$getcontents = file_get_contents("http://example.com/script.php");
if(strpos($getcontents , 'false') !== false) {
check_for_false();
} else if(strpos($getcontents , 'true') !== false) {
echo "finished";
} else {
echo "response didn't contain \"true\" or \"false\"";
}
}
This function should keep calling itself until $getcontents contains the word true, and does not contain false.

Using php how to match boolean value with word in sentence?

I have a response from aws like this with boolean value :
$string=/vasff/fdsfsdf:boolean true
and $string=/sadasff/fdsfsdf:boolean false
in their documentation, they had written a response in boolean.// Success? (Boolean, not a CFResponse object).
Their documentaion is https://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_doesObjectExist
how to match value is true or false? I tried this code but it always returns true.
if(stripos($string, true) !== false){
echo "true";
}
else{
echo "false";
}
EDIT
Just wanted to thank everyone for their good answers on this.
This code is working fine for aws s3 bucket response.
if($response == FALSE){
return false; }else{
return true;
}
if you mean by this :
$string=/vasff/fdsfsdf:boolean true
this :
$string='/vasff/fdsfsdf:boolean true';
you can use this code : use true with double quote
$string='/vasff/fdsfsdf:boolean true';
if(stripos($string, "true") !== false){
echo "true";
}
else{
echo "false";
}
Edit :
use your function paramaters intead of ....
if(doesObjectExist(....)){
echo "true";
}else{
echo "false";
}
You can try this:
preg_match_all("/:boolean (.+)/", $string, $mc);
if(isset($mc[1]) && isset($mc[1][0])){
if($mc[1][0] == "false"){
echo $mc[1][0] . " == false";
}
else{
echo $mc[1][0] . " == true";
}
}

php comparison function not working as expected

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"
}

Check if URL has certain string and match position with PHP

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.

Check if page returns true or false

I am trying to verify a Minecraft username is paid or not.
By typing in the username at the end of the URL, it returns true or false.
$input = 'Notch';
function checkPlayer($player) {
$mcURL = 'http://www.minecraft.net/haspaid.jsp?user=';
$auth = file_get_contents($mcURL . $player);
if ($auth === true) {
echo $player. ' is valid';
} else {
echo $player. ' is not valid';
}
}
checkPlayer($input);
But it doesn't return true. By going to the page http://www.minecraft.net/haspaid.jsp?user=Notch, it does return true. How do I properly check? I think file_get_contents is the wrong function to use for this matter. I'm not sure though.
change this line :
if ($auth === true) {
with
if (trim($auth) == "true") {

Categories