check last part of URL with php - php

I'm working with Zend Framework. I want to check last part of link. my link is http://localhost/sports/soccer/page_id/776543233242
my last part of URL must have 12 or 11 digit , and I want first of that part start with 8 if 11 digit and start with 7 if 12 digit.
public function detailAction ()
{
$uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
if (substr(substr($uri, -12),1)=='7'){
echo "successfull";
}
else if (substr(substr ($uri , -11),8)=='0'){
echo "succ";
}
else {
echo "failed";
}
}

This should work for you:
<?php
$url = "http://localhost/sports/soccer/page_id/776543233242";
$part = basename($url);
if(strlen($part) == 11 && $part[0] == 8 || strlen($part) == 12 && $part[0] == 7)
echo "yes";
else
echo "no";
?>
Output:
yes

Use basename() function in php to get your last part of the url and then count the string.Check the no of word using strlen. Use the code below
public function detailAction ()
{
$url = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
$url = "http://localhost/sports/soccer/page_id/776543233242";
$basename = basename($url);
if(strlen($basename) == 11 && $basename[0] == 8 || strlen($basename) == 12 && $basename[0] == 7){
echo "succ";
}
else{
echo "failed";
}
}
Hope this helps you

See parse_url. You can then get the path and split it on /.
function isValidUrl($url)
{
$elements = parse_url($url);
if ($elements === false) {
return false;
}
$lastItem = end(explode("/", $elements['path']);
return ((strlen($lastItem) == 12 && $lastItem[0] == '7') || (strlen($lastItem) == 11 && $lastItem[0] == '8'));
}

If you only want to get the number form your url I suggest you to use regex. in this case:
$url="http://localhost/sports/soccer/page_id/776543233242";
$regex = "/[0-9]+/";
preg_match_all($regex, $url, $out); //$out will be an array storing the mathcing strings, preg_match_all creates this variable to us
var_dump($out);
This code outputs the $out. It will look like this:
array(1) {
[0]=>
array(1) {
[0]=>
string(12) "776543233242"
}
}
I hope it will help you.

Obviously the part of the url desired corresponds to the page_id parameter.
With Zend Framework, you can do something like this to get the value of this parameter:
$page_id = $this->getRequest()->getParam('page_id');
if(strlen($page_id) == 11 && $page_id[0] == 8
|| strlen($page_id) == 12 && $page_id[0] == 7)
echo "yes";
else
echo "no";

Related

PHP check if a string contains a specific character [duplicate]

This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 1 year ago.
I have 2 strings $verify and $test. I want to check if $test contains elements from $verify at specific points. $test[4] = e and $test[9] = ]. How can I check if e and ] in $verify?
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if ($test[4] == $verify AND $test[9] == $verify) {
echo "Match";
} else {
echo "No Match";
}
Use strpos() https://www.php.net/manual/en/function.strpos.php
https://paiza.io/projects/n10TMV4Ate8-vtzioDrsMg
<?php
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if (strpos($verify, $test[4]) !== false && strpos($verify, $test[9]) !== false) {
echo "Match";
} else {
echo "No Match";
}
?>
As of PHP8 you can use str_contains()
<?php
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if (str_contains($verify, $test[4]) && str_contains($verify, $test[9])) {
echo "Match";
} else {
echo "No Match";
}
?>
Thanks to #symlink for the code snippet I butchered

Compare first 4 characters of a string PHP

The problem is with this line:
if $var LIKE '1800%';
and I'm not sure how to fix it. Thanks.
<?php
//check to see if account number is like 1800*
if (isset($_POST['acct_number'])) {
$var = $_POST['acct_number'];
if $var LIKE '1800%'; {
//stop the code
exit;
} else {
echo 'normal account number';
}
}
?>
You need PHP not MySQL. For 1800% just check that it is found at position 0:
if(strpos($var, '1800') === 0) {
//stop the code
exit;
} else {
echo 'normal account number';
}
If it can occur anywhere like %1800% then:
if(strpos($var, '1800') !== false) {
//stop the code
exit;
} else {
echo 'normal account number';
}
Use substr function to get first 4 characters and compare it with 1800.
if(substr($var, 0, 4) == '1800')
{
// your code goes here.
}
``
Another way could be to use strpos()
if (strpos($var, '1800') === 0) {
// var starts with '1800'
}
I would use a regular expression for this preg_match('/^1800.+/', $search, $matches);

if condition with foreach loop for array in php

is it any way to stop this repeated data.
if ($employees_csa[0]->csa_taken == 2 && $employees_csa[1]->csa_taken == 2 && $employees_csa[2]->csa_taken == 2 && $employees_csa[3]->csa_taken == 2 && $employees_csa[4]->csa_taken == 2 && $employees_csa[5]->csa_taken == 2 && $employees_csa[6]->csa_taken == 2 && $employees_csa[7]->csa_taken == 2) {
echo "data";
}
i tried for key range(0 , 8)
like this
foreach (range(0, count($employees_csa)) as $number) {
if ($employees_csa[$number]->csa_taken == 2) {
echo "data";
}
}
i tried that way not get any succes. i any another way to write easy condition.
You can loop arrays out of the box:
$all_taken = true;
foreach ($employees_csa as $employee) {
if ($employee->csa_taken != 2) {
$all_taken = false;
break;
}
}
if ($all_taken) {
echo 'data';
}
Another approach would be array_reduce() but this doesn't abort looping when there's already an answer:
$all_taken = array_reduce($employees_csa, function ($all_taken, $employee) {
if ($employee->csa_taken != 2) {
return false;
}
return $all_taken;
}, true);
if ($all_taken) {
echo 'data';
}
Alternatively, you could do it like this using array_column to pull out all the csa_taken properties, then reducing to 1 item if they are all the same with array_unique() and then checking that the same value is the expected number 2 with reset().
$csa_taken = array_column($employees_csa, 'csa_taken');
if (reset($csa_taken) === 2 && count(array_unique($csa_taken)) === 1) {
echo 'data';
}
Reusable function version: https://3v4l.org/4kYiE
A simple for-loop could work
$condition_met=true;
for($i=0;$i<8;++$i){
if( $employees_csa[$i]->csa_taken != 2){
$condition_met=false;
break;
}
}
if($condition_met===true){
//success
}
else{
//fail
}
A simple method could be done like
foreach($employees_csa as $singleEmployee){
if($singleEmployee->csa_taken == 2){
echo "data";
}
}

Nesting if else statements in PHP to validate a URL

I'm currently writing up a function in order to validate a URL by exploding it into different parts and matching those parts with strings I've defined. This is the function I'm using so far:
function validTnet($tnet_url) {
$tnet_2 = "defined2";
$tnet_3 = "defined3";
$tnet_5 = "defined5";
$tnet_7 = "";
if($exp_url[2] == $tnet_2) {
#show true, proceed to next validation
if($exp_url[3] == $tnet_3) {
#true, and next
if($exp_url[5] == $tnet_5) {
#true, and last
if($exp_url[7] == $tnet_7) {
#true, valid
}
}
}
} else {
echo "failed on tnet_2";
}
}
For some reason I'm unable to think of the way to code (or search for the proper term) of how to break out of the if statements that are nested.
What I would like to do check each part of the URL, starting with $tnet_2, and if it fails one of the checks ($tnet_2, $tnet_3, $tnet_5 or $tnet_7), output that it fails, and break out of the if statement. Is there an easy way to accomplish this using some of the code I have already?
Combine all the if conditions
if(
$exp_url[2] == $tnet_2 &&
$exp_url[3] == $tnet_3 &&
$exp_url[5] == $tnet_5 &&
$exp_url[7] == $tnet_7
) {
//true, valid
} else {
echo "failed on tnet_2";
}
$is_valid = true;
foreach (array(2, 3, 5, 7) as $i) {
if ($exp_url[$i] !== ${'tnet_'.$i}) {
$is_valid = false;
break;
}
}
You could do $tnet[$i] if you define those values in an array:
$tnet = array(
2 => "defined2",
3 => "defined3",
5 => "defined5",
7 => ""
);

if else simple beginner issue

Good day guys,
I've made a sweet favorites function with php mysql and ajax, and its working great. Now I want to show 'favorite' when favorite = 0 and show 'unfavorite' when favorite = 1
if ($favorites == 0) {
$favorite = 'Favorite';
}
if ($favorites == 1) {
$unfavorite = 'unFavorite';
}
and echo it in the row as :
<div id="favorites">' .($favorite). ' ' .($unfavorite). '</div>
The problem is: when favorite = 0, both $favorite and $unfavorite are being shown. When favorite = 1 only $unfavorite is being shown correctly. Of course it should be $favorite OR $unfavorite. I assume the problem is clear and simple to you, please assist :)
Thanks in advance
It's easier to use just one variable:
$text = ''
if ($favorites == 0) {
$text = 'Favorite';
} else {
$text = 'unFavorite';
}
...
echo $text;
If you want to check $favorite, you are using the wrong variable in your control statement. Also, it is better coding practice to use elseif rather than if for that second if. One more thing: it's easier to manage one resulting variable.
$output = "";
if ($favorite == 0) {
$output = 'Favorite';
}
elseif ($favorite == 1) {
$output = 'unFavorite';
}
...
echo $output; // Or whatever you want to do with your output
Is $favorites an integer?
Anyway try using three equal signs (===) or else instead of the second if:
if ( $favorites === 0 )
{
// ...
}
else // or if ($favorites === 1)
{
// ...
}
You're making a toggle, so you only need one variable:
if(empty($favourites)){
$fav_toggle = 'Favorite';
} else {
$fav_toggle = 'unFavorite';
}
echo $fav_toggle;
Same code is working on me if I assigned $favorites = 0; or $favorites = 1;
You can also use if else
$favorites = 1;
if ($favorites == 0) {
$favorite = 'Favorite';
}
else if ($favorites == 1) {
$unfavorite = 'unFavorite';
}

Categories