0

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 :)

2 Answers 2

2

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'; 
 }
Sign up to request clarification or add additional context in comments.

Comments

0

If you know how long and the exact characters for the first part of the string, you can simply use substr().

$urlLength = 23; // http://www.someurl.com/ have 23 characters
$urlText =  "http://www.someurl.com/"; // What is the text you want to validate?
if (substr($string, 0, $urlLength) == $urlText) {
    echo "Valid.";
}
else {
    echo "Not valid.";
}

The substr() function first take the string ($string). Then if you want the check to start immediately, the second parameter will be '0'. Next is the number of characters you want to validate.

4 Comments

unfortunately, the actual variable link is not known ahead of time. Its entirely retrieved from a database from data input by another user in the system - meaning the url could be any url and any number of characters long.
My belief is I am going to have to do some sort of php regex to get all the characters before the 3rd "/" in the url, set those to a variable, do a count on that variable, and then mix your code with that, but regex is so over my head.....
I don't really understand you problem. The code I wrote above do not need you to count for the characters in $string. All you need to do is to provide the first part of the URL you want to validate ("someurl.com/") and then count how long the URl is.
Neither answer was right or wrong. As I showed in the final update/code, first I had to use regex to get the URI as the URI is not known. Its a variable passed from the database. After getting the URI through regex, the only way to know how long the URI is, would be to set a variable, and then count the length of the variable srting with strlen. Otherwise how would I know how long the URI is? I selected the answer I did because although it also didn't explain any of the regex part, the last extra strlen count on the string was not required. Both answers would have worked after I did the regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.