Bruce Bruce - 5 months ago 9
Javascript Question

match user input url to url in php variable

There is a variable

$string = 'http://www.someurl.com/somerandom/page.php';


There is an input

<input type="text" class="theurl" />


I need to verify the first part of the data the user adds to the input matches the first part of the string. Meaning the user data should begin with:

http://www.someurl.com/


But not verify whatever comes after that /, meaning the user can change the

somerandom/page.php


part of things. I am very much still learning php and have come a long way, but this is WAY over my head.

Note I do not want to prefill the data for the user and do not know the variable link ahead of time. I just want to match and verify. Perhaps a mix of jquery/js and php?

I don't even know where to begin. Help? Perhaps example code specific to my issue?

Update / Final

So far here is what i have figured out

$re = "/http:\\/\\/([^\\/\\r\\n]+)/";
$string = $baffiliate;
$newstring = preg_match($re, $string, $matches);
foreach($matches as $key=>$value)
{
$value = $value;
}
if(strpos($_POST['affiliate'], $value) === false) {
echo 'error';
}else{
...do something
}


I opted to use this solution as it didn't require an extra count of thee variable during test :)

Answer

You can use STRPOS.

 $string = 'http://www.someurl.com/somerandom/page.php';
 if(strpos($string, 'http://www.someurl.com/') >= 0) {
    echo 'valid';   
 } else {
    echo 'invalid'; 
 }