Conditional with htmlspecialchars - php

I am using this to take a special chars from the URL:
echo '' . htmlspecialchars($_GET["concert"]);
and I need to create a conditional in order to put one title and one image depending on what is on this special chars from the URL.
So something like:
if htmlspecialchars "concert" == "fanpass" do this, else do this.
Any idea how can I put a conditional statement with this?

There seems to be a confusion in the use of the words special chars. In the meaning of the PHP function htmlspecialchars, a special character is a less-than symbol (<), a greater-than symbol (>), ampersand (&) and possibly a single or double quote.
You should not apply htmlspecialchars when comparing a string with another normal string.
So, why not just:
$concert = $_GET["concert"];
if ($concert === "fanpass") {
// do one thing
} else if ($concert === "studentdiscount") {
// do another thing
} else {
// any other value
echo htmlspecialchars($concert);
// ...
}
Going by the variable names, it seems you want to use the concert parameter for something else, like a discount condition.
In that case you could start to use a new URL parameter, like for instance rate:
$concert = $_GET["concert"];
$rate = "(none)";
if (isset($_GET["discount"]) {
$rate = $_GET["discount"];
};
if ($rate === "fanpass") {
// do one thing
} else if ($rate === "studentdiscount") {
// do another thing
} else {
// any other value
// ...
}
echo "Your concert: {htmlspecialchars($concert)}<br>";
echo "Your rate: {htmlspecialchars($rate)}<br>";

Related

preg_match / word boundery only works to a limited extent

I would like to compare a text from its textarea with a come-separated keyword list and trigger a specific action if one or more hits are found.
Unfortunately, my script only works to a limited extent and triggers my function even if a character string, e.g. B. "ee" or "work" is identified.
Example for the keyword list:
"FREE Shipping,times,50% OFF,Backpack,Marketing,working,Owner,CEO"
Example of a text from the textarea:
"it seems that the function is not working correctly"
$messageTemp = explode(",",$GLOBALS['Message']);
foreach ($messageTemp as $valueK) {
if (preg_match("~\b(.+)$valueK\b~",$GLOBALS['Keyword']) !== 0 ) {
$GLOBALS['Key'] = 1;
};
};
I'm grateful for every hint
To put an arbitrary string inside reg exp you have to escape it first. Also, I added ignore case option. And removed the (.+) part.
foreach ($messageTemp as $valueK) {
$reg ="~\b".preg_quote($valueK, '/')."\b~i";
if (preg_match($reg, $keyword) !== 0) {
$GLOBALS['Key'] = 1;
}
}

Return true/false if word in URL matches specific word

I currently use:
if(strpos($command->href,§current_view) !== false){
echo '<pre>true</pre>';
} else {
echo '<pre>false</pre>';
}
$command->href will output something like this: /path/index.php?option=com_component&view=orders Whereas
§current_view is outputting orders. These outputs are dynamically generated, but the scheme will always be the same.
What I need to do is return true/false if the words from $current_view match the view=orders in the URLs from $command->href. The issue with my code is, that it doesnt match anything.
What is the correct way to do this?
Please note that the $command->href and the whole code is inside a while function, that pass multiple URLs and this only needs to match the same ones.
Breaking it down to a simple example, using your code and variable values.
$current_view = 'orders';
$command = '/path/index.php?option=com_component&view=orders';
if(strpos($command,$current_view) !== false){
echo '<pre>true</pre>';
}
else {
echo '<pre>false</pre>';
}
The oputput is "true".
Now, go and debug the REAL values of $command->href and $current_view...
I'm pretty confident that the values are not what you think they are.
Does something like:
if(substr($command->href, strrpos($command->href, '&') + 6) == $current_view)
accomplish what you are after?
To explain, strpos get the last instance of a character in a string (& for you, since you said it always follows the scheme). Then we move over 6 characters to take "&view=" out of the string.
You should now be left with "orders" == "orders".
Or do you sometimes include some arguments after the view?
Try parsing url, extracting your view query string value and compare it to $current_view
$query= [];
parse_str(parse_url($command->href)['query'], $query);
if($current_view === $query["view"])
echo '<pre>true</pre>';
} else {
echo '<pre>false</pre>';
}

Regular expression to check repeating character or digit, check lowercase,uppercase,capital

please help me..i'm stuck in here..
What i actually want is to check the password from repeating single character or digit.
Requirement for repeating
aaaa = false,
abbb = false
abag = false
a33f = false
abcd1234 = true
there is only once for a character should have in password. If more than once repeated, error returns. So hard to explain.
this is my draft code.
1)first i need to check whether the global configuration for repeating character is allowed or not, if yes my password can have repeating char or digit otherwise it would't. After this, i need to check whether the global configuration for lowercase,uppercase or capitals allowed or not.
if($globalCOnf['repeat_pass']=="yes")//allowed
{
//do nothing
}
else //not allowed
{
//stuck here :(
if(preg_match('/(.)\1{1,}/',$user_input_pass)) //only check "aaaa" not "aba"
{
echo "change password";
}
else
{
if($globalCOnf['having_lower_upper_capital']=="yes")//allowed
{
//do nothing
}
else
{
// can't continue
}
}
}
You can use array_count_values for this (An alternate regex free solution)
<?php
$password = 'abcdfa';
if(max(array_count_values(str_split($password)))>1)
{
echo "Choose another password as words you can't have repeatable characters";
}
OUTPUT:
Choose another password as words you can't have repeatable characters
You get that output because a is repeated two times.
Answer for the question.
if($globalCOnf['repeat_pass']=="yes")//allowed
{
//do nothing
}
else //not allowed
{
if(max(array_count_values(str_split($user_pass)))>1)
{
echo "change your password now!!!";
}
else
{
if($globalCOnf['having_lower_upper_capital']=="yes")//allowed
{
//do nothing
}
else
{
if(preg_match('/[A-Z]/', $user_pass))
{
echo "Can't use uppercase";
}
}
}
}
100% working.. :)
Try something like this -
(\d+).*\1
If you get any match there is a repeated character.
Just allow zero or more characters between two duplicate characters. If there is a match, then the string failed to pass the validation.
Code: (Demo)
$passwords=['aaaa','abbb','abAg','a33f','abcd1234'];
foreach($passwords as $pass){
echo "$pass: ";
if(!preg_match('/([a-zA-Z\d]).*\1/',$pass)){
echo "valid\n";
}else{
echo "failed\n";
}
}
Output:
aaaa: failed
abbb: failed
abAg: valid
a33f: failed
abcd1234: valid
Or as one-line: echo preg_match('/([a-zA-Z\d]).*\1/',$pass)?'failed':'valid'
Using this type of pattern is much more direct that generating a temporary array of characters and counting their occurrences and checking the highest count.

Returning a top level domain with a period at the end in php

Basically the problem I am having is I need to write this function that can take a URL like www.stackoverflow.com and just return the "com". But I need to be able to return the same value even if the URL has a period at the end like "www.stackoverflow.com."
This is what I have so far. The if statement is my attempt to return the point in the array before the period but I dont think I am using the if statement correctly. Otherwise the rest of the code does exactly what is supposed to do.
<?php
function getTLD($domain)
{
$domainArray = explode("." , $domain);
$topDomain = end($domainArray);
if ($topDomain == " ")
$changedDomain = prev(end($domainArray));
return $changedDomain;
return $topDomain;
}
?>
Don't use a regex for simple cases like that, it is cpu costly and unreadable. Just remove the final dot if it exists:
function getTLD($domain) {
$domain = rtrim($domain, '.');
return end(explode('.', $domain));
}
The end function is returning an empty string "" (without any spaces). You are comparing $topDomain to single space character so the if is not evaluating to true.
Also prev function requires array input and end($domainArray) is returning a string, so, $changedDomain = prev(end($domainArray)) should throw an E_WARNING.
Since end updates the internal pointer of the array $domainArray, which is already updated when you called $topDomain = end($domainArray), you do not need to call end on $domainArray inside the if block.
Try:
if ($topDomain == "") {
$changedDomain = prev($domainArray);
return $changedDomain; // Will output com
}
Here is the phpfiddle for it.
Use regular expressions for something like this. Try this:
function getTLD($domain) {
return preg_replace("/.*\.([a-z]+)\.?$/i", "$1", $domain );
}
A live example: http://codepad.org/km0vCkLz
Read more about regular expressions and about how to use them: http://www.regular-expressions.info/

PHP regex numbers in url

I've got a blog, I want to display ADS or special text on the MAIN page but not on any other page with the URI /?paged=[number]. I've tried the standard regex /^[0-9].$/ but it fails to match.
I'm not sure what I've done wrong or how to go about doing this
if (substr_count($_SERVER[REQUEST_URI], "/") > 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=1") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=2") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=3") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=4") == 0 AND
substr_count($_SERVER[REQUEST_URI], "/?paged=5") == 0) {
echo "display special text, banners, and other stuff";
}
This is how I'm doing it currently, but I don't want to do thousands of these...
Can you not just check for the presence of paged in the GET array?
if(!isset($_GET['paged'])) {
// You know this is the main page.
}
Try this:
if (preg_match('#paged=\d+#', $_SERVER[REQUEST_URI]) {
echo "display special text, banners, and other stuff";
}
Regex: /^[0-9].$/ would be correct for "3k" string. Analize this patterns
/page=(\d+)/
/page=([1-5])/
/^\/\?page=([1-5])$/
/page=(?<page>[1-5])/
Why not using the regexp in the GET parameter ?
<?php
$regexp = "/^(\d+)+$";
if (preg_match($regexp, $_GET['paged'])) {
#...your code
} else {
#...your code
}
Or (if you want to use the entire string) try this:
<?php
$regexp = "/^(\/\?paged)+=(\d+)+$/";

Categories