Lets say i capture the url as $url = $_SERVER['REQUEST_URI'] or $_GET['q'].
How can i check for the condition if the url contains a piece of text say "x"?
What regex would be required for it
If you use preg_match with a variable value, make sure you use preg_quote to escape any special characters:
$look_for = "x";
if (preg_match( "/".preg_quote($look_for, "/")."/i" , $url) ) {
...
You can also use the strpos() function
http://php.net/manual/en/function.strpos.php
This is one example from there:
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
In addition to the functions for finding the string, you may also want to check out Drupal's arg() function: http://api.drupal.org/api/drupal/includes--path.inc/function/arg/6
I think preg_match would serve you well:
if (preg_match( "/x/i" , $url) ) {
}
If your working with a url from the wild you might want to use php's parse_url(), http://www.php.net/manual/en/function.parse-url.php , that will make it easy to work with. If it is a drupal URL your probably going to want to use Drupal's arg() function, http://api.drupal.org/api/drupal/includes--path.inc/function/arg/6 , but it can have issues with the Pathauto module
Related
i have a variable and i want to use php to check if it contains a group of characters
i would like the code to be like this
$groupofcharacters = ["$","#","*","("];
if($variable contains any of the letters in $groupofcharacters){
//do something}
i know that this will need the use of strpos() function but how can i use the strpos function to check if a variable contains a group of characters without me having to create a strpos() function for all the characters that i want to check for.
please if you don't understand you can tell me in the comments
Best way to solve your issue is by using RegEx. Try this:
<?php
$variable = 'Any string containing $*(#';
$sPattern = '/[$#*(]/';
if (preg_match($sPattern, $variable)) {
// Do something
}
You can use strpbrk to achieve this. The doc says:
strpbrk — Search a string for any of a set of characters
Returns a string starting from the character found, or FALSE if it is not found.
Snippet:
<?php
if(strpbrk($variable,"$#*(") !== false){
// your logic goes here
}
check if it has any char. Using strpos
First you need to combine the array as string after check ot if there's one of them in the
$groupofcharacters = ["$","#","*","("];
$strs = implode("", $groupofcharacters);
foreach(str_split($variable) as $s) {
if (strpos($s, $strs)) {
echo "it contains "; continue;
}
}
I am trying to find whether a string contains a certain text or not using strstr()
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr('/image/', $t));
exit;
But this gives false. Why is it giving fasle? How to fix it?
Your parameters are inverted (see strstr). This is the correct way of using it:
strstr($t, '/image/');
you should use strpos , faster, less resources, from the manual with your vars
<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
$findme = '/image/';
$pos = strpos($t, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?>
try like this instead
<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr($t, '/image/'));
exit;
?>
You can see the documentation of the function.
Validates the syntax.
php/documentation/function.strstr.php
The correct use would: var_dump(strstr($t, '/image/'));
I have some words with | between each one and I have tried to use preg_match to detect if it's containing target word or not.
I have used this:
<?php
$c_words = 'od|lom|pod|dyk';
$my_word = 'od'; // only od not pod or other word
if (preg_match('/$my_word/', $c_words))
{
echo 'ok';
}
?>
But it doesn't work correctly.
Please help.
No need for regular expressions. The functions explode($delimiter, $str); and in_array($needle, $haystack); will do everything for you.
// splits words into an array
$array = explode('|', $c_words);
// check if "$my_word" exists in the array.
if(in_array($my_word, $array)) {
// YEP
} else {
// NOPE
}
Apart from that, your regular expression would match other words containing the same sequence too.
preg_match('/my/', 'myword|anotherword'); // true
preg_match('/another/', 'myword|anotherword'); // true
That's exactly why you shouldn't use regular expressions in this case.
You can't pass a variable into a string with single quotes, you need to use either
preg_match("/$my_word/", $c_words);
Or – and I find that cleaner :
preg_match('/' .$my_word. '/', $c_words);
But for something as simple as that I don't even know if I'd use a Regex, a simple if (strpos($c_words, $my_word) !== 0) should be enough.
You are using preg_match() the wrong way. Since you're using | as a delimiter you can try this:
if (preg_match('/'.$all_words.'/', $my_word, $c_words))
{
echo 'ok';
}
Read the documentation for preg_match().
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/");
I want to know if how can I check I string (specifically an ip address) if that ip address has the string of x
For example
$ip = "66.124.61.23" // ip of the current user
$x = "66.124" // this is the string what I want to check in my $ip.
So how can I check $ip if it has the string of $x?
Please do leave a comment if you are having a hard time understanding this situation.
Thank you.
Use strstr()
if (strstr($ip, $x))
{
//found it
}
See also:
stristr() for a case insenstive version of this function.
strpos() Find the first occurrence of a string
stripos() Find the position of the first occurrence of a case-insensitive substring in a string
You can also use strpos(), and if you're specifically looking for the beginning of the string (as in your example):
if (strpos($ip, $x) === 0)
Or, if you just want to see if it is in the string (and don't care about where in the string it is:
if (strpos($ip, $x) !== false)
Or, if you want to compare the beginning n characters, use strncmp()
if (strncmp($ip, $x, strlen($x)) === 0) {
// $ip's beginning characters match $x
}
Use strstr()
$email = 'name#example.com';
$domain = strstr($email, '#');
echo $domain; // prints #example.com
Based on $domain, we can determine whether string is found or not (if domain is null, string not found)
This function is case-sensitive. For case-insensitive searches, use stristr().
You could also use strpos().
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
Also read SO earlier post, How can I check if a word is contained in another string using PHP?
Use strpos().
if(strpos($ip, $x) !== false){
//dostuff
}
Note the use of double equals to avoid type conversion. strpos can return 0 (and will, in your example) which would evaluate to false with single equals.