Match URL link and ignore the page behind - php

I have this code that will match the url and than do something, its working good but when the url has some page behind like /board/users/somename/replies/page/2/ the code will not work. How can I only detect this part of url /board/users/somename/replies/ and ignore the rest behind? Thanks for helping.
$pageURL = $_SERVER['REQUEST_URI'];
$myurl = '/board/users/somename/replies/';
if (strtolower($pageURL) == strtolower($myurl) ) {
echo "right";
}else{
echo "wrong";
}

You just need to match the start of the string, like this:
if ( strpos(strtolower($pageURL), strtolower($myurl)) === 0 )
You also have to ensure that the leading and trailing slashes match up, so I would suggest you modify the first two lines of your code like this:
$pageURL = '/' . trim($_SERVER['REQUEST_URI'], '/');
$myurl = '/board/users/somename/replies';
EDIT: I had got the slashes wrong. That's fixed now.

simply try a regular expression
if preg_match('/'.$myurl.'/', $pageUrl) {}
this will match any url that has $myurl in it. preg_match returns 1 if the regular expression matches, and 0 if it doesn't.
if you want to exclude the URL's with page/ in it, try a negative lookahead via regex
if preg_match('/(?!'.$myurl.'page\/)'.$myurl.'/', $pageUrl) {}
The negative lookahead makes sure myurl/page/ is not in the URL and then you simply match for your url.
edit: for further information on preg_match check the php manual at preg_match

You can also use PHP stristr function to match the beggining of a string. (case-insensitive)
Try this:
$pageURL = $_SERVER['REQUEST_URI'];
$myurl = '/board/users/somename/replies/';
if (stristr($pageURL, $myurl) !== FALSE) {
echo "right";
} else {
echo "wrong";
}
I hope it helps!

Related

PHP regex: How to remove ?file in url?

My url like this:
http://mywebsite.com/movies/937-lan-kwai-fong-2?file=Rae-Ingram&q=
http://mywebsite.com/movies/937-big-daddy?file=something&q=
I want to get "lan-kwai-fong-2" and "big-daddy", so I use this code but it doesn't work. Please help me fix it ! If you can shorten it, it is so great !
$url= $_SERVER['REQUEST_URI'];
preg_replace('/\?file.*/','',$url);
preg_match('/[a-z][\w\-]+$/',$url,$matches);
$matches= str_replace("-"," ",$matches[0]);
First there are issue with your code which im going to go over because they are general things:
preg_replace does not work by reference so you are never actually modifying the url. You need to assign the result of the replace to a variable:
// this would ovewrite the current value of url with the replaced value
$url = preg_replace('/\?file.*/','',$url);
It is possible that preg_match will not find anything so you need to test the result
// it should also be noted that sometimes you may need a more exact test here
// because it can return false (if theres an error) or 0 (if there is no match)
if (preg_match('/[a-z][\w\-]+$/',$url,$matches)) {
// do stuff
}
Now with that out of the way you are making this more difficult than it needs to be. There are specific function for working with urls parse_url and parse_str.
You can use these to easily work with the information:
$urlInfo = parse_url($_SERVER['REQUEST_URI']);
$movie = basename($urlInfo['path']); // yields 937-the-movie-title
Just replace
preg_replace('/\?file.*/','',$url);
with
$url= preg_replace('/\?file.*/','',$url);
Regex works, and parse_url is the right way to do it. But for something quick and dirty I would usually use explode. I think it's clearer.
#list($path, $query) = explode("?", $url, 2); // separate path from query
$match = array_pop(explode("/", $path)); // get last part of path
How about this:
$url = $_SERVER['REQUEST_URI'];
preg_match('/\/[^-]+-([^?]+)\?/', $url, $matches);
$str = isset($matches[1]) ? $matches[1] : false;`
match last '/'
match anything besides '-' until '-'
capture anything besides '?' until (not including) '?'

PHP regex parse my visible link

I want to check my link in a website, but I also want to check is it visible. I wrote this code:
$content = file_get_contents('tmp/test.html');
$pattern = '/<a\shref="http:\/\/mywebsite.com(.*)">(.*)<\/a>/siU';
$matches = [];
if(preg_match($pattern, $content, $matches)) {
$link = $matches[0];
$displayPattern = '/display(.?):(.?)none/si';
if(preg_match($displayPattern, $link)) {
echo 'not visible';
} else {
echo 'visible';
}
} else {
echo 'not found the link';
}
It works, but not perfect. If the link is like this:
<a class="sg" href="http://mywebsite.com">mywebsite.com</a>
the fist pattern won't work, but if I change the \s to (.*) it gives back string from the first a tag. The second problem is the two pattern. Is there any way to merge the first with negation of the second? The merged pattern has 2 results: visible or not found/invisible.
I'll try to guess.
You are having a problem if your code(one that you fetch with file_get_contents) looks like this
<a class="sg" href="http://mywebsite.com">mywebsite.com</a>
.
.
.
mywebsite.com
Your regex will return everything from first </a> tag because dot matches a new line(I guess you need it turned on, but if you dont, its 's' flag, so remove it)
Therefore
.*
will keep searching everything, so you need to make it greedy
(when its greedy it will stop searching once it finds what its looking for), like this
.*?
Your regex should look like this then
<a.*?href="http:\/\/mywebsite.com(.*?)">(.*?)<\/a>

pregmatch for url. regex

I have a regex that validate a specific url but it not really working. I want to validate urls like this -----> https : // example.co.nz/#![RANDOM_KEYS_HERE].
I want to do it only with https. Most importantly, the input of the user need to match https : // example.co.nz/#! but after the #!, the user can put anything he like.
Here is the code:
I know that the code is fked up xD I have a basic knowledge in that lol
#^https://example+\.[co\.nz][a-z0-9-_.]+\.[a-z]{2,4}#i
If anyone could help me to do it, it would be great! thanks!
Erm... not even close. Your regex reads as follows:
Starting from the beginning of the string...
Match literally https://exampl
Match one or more e
Match a literal .
Match one of any of these: cnoz.
Match one or more of these: a-z0-9-_.
Match a literal .
Match between 2 and 4 letters
This is nothing like what you're looking for. After all, I don't think you want this to pass:
https://exampleeeeeeeeeeee.complete.and.total.failure.-_-.lol
Instead, try this:
(^https://example\.co\.nz/#!(.*))
This regex reads as follows:
Starting from the beginning of the string...
Match literally https://example.co.nz/#!
Capture everything thereafter
Try this out:
^https:\/\/example\.co\.nz\/\#\!(.*)$
The parentheses at the end will do a sub-expression match which should allow you to pull out the ID.
if (preg_match('/^https:\/\/example\.co\.nz\/\#\!(.*)$/', $searchString, $matches)) {
$id = $matches[1];
}
if (preg_match('%^https://example\.co\.nz/#!(.+)$%i', $subject)) {
# Successful match
} else {
# Match attempt failed
}
Or you can get your [RANDOM_KEYS_HERE] part with this one
if (preg_match('%^https://example\.co\.nz/#!(.+)$%i', $subject, $regs)) {
$result = $regs[0];
} else {
$result = "";
}
You don't need regexp there. You just need to find out if string starts with some substring. Check this:
if(strpos($url, 'https://example.co.nz/#!')===0)
{
echo 'url is OK';
}
else
{
echo 'url is wrong';
}
http://www.php.net/manual/en/function.strpos.php
I hope this helps.

match part of a url

I would like to get all matches for any url's that have index.php?route=forum/ in them
Example urls to filter are:
http://test.codetrove.com/index.php?route=forum/forum
http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=2
So i need the match to be true if it contains index.php?route=forum/ the http and domain can be anything like http or https or any domain.
Any idea's?
Rather than using a baseball bat to bludgeon a spider, take a look at strpos().
$string = "index.php?route=forum/";
if (strpos($url, $string) !== false) {
//we have a match
}
You can use regex :
/index\.php\?route=forum\/.*/
Or with the $_GET variable
if(preg_match('/forum\/.*/', $_GET['route'])) {
echo 'yahoo';
}
One possibility is to use the php strpos function documented here
$IsMatch = strpos ( $url , "index.php?route=forum/");

Determining if a URL contains a certain hostname

I need to make a regular expression for php preg_match that does the following matching.
This is the function
function isValidURL($url,$searchfor){
return preg_match("/\b.$searchfor \b/i", $url);
}
I need to find the somedomain.com in the following
Possible Strings entering the function
http://www.somedomain.com
http://somedomain.com
http://www.somedomain.com/anything
http://somedomain.com/anything
http://anything/somedomain.com
So I need a regular expression that does this
http://www.somedomain.com Will Match
http://somedomain.com Will Match
http://www.somedomain.com/anything Will Match
http://somedomain.com/anything Will Match
but
http://anything/somedomain.com Will NOT match
What about using parse_url()?
if( strpos(parse_url($url, PHP_URL_HOST), 'somedomain.com') !== false )
{
// hostname contains 'somedomain.com'.
}
Try this...
$url = "http://komunitasweb.com/";
if (preg_match('/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):? (d+)?/?/i', $url)) {
echo "Your url is ok.";
} else {
echo "Wrong url.";
}
Copied from a google searh on "php url regular expression". Check google out, awesome tool. :-)
All this requires is a placeholder for the URL beginning. Excluding slashes with a negated character class [^/] might already be sufficient:
function isValidURL($url,$searchfor){
return preg_match("~http://[^/\s]*\.$searchfor(/|$|\s)~i", $url);
}
Note that this fails some edge cases, like user:pw# pairs. And no idea if your $searchfor was supposed to contain the TLD already. Also don't forget to preg_quote it.

Categories