PHP Strip numbers - php

Hello I need to put dot between first number and second number my result is:
11 I need to make him like this 1.1 how ?
if ( strlen($model->rate) == 2 )
{
}

$model->rate = $model->rate[0] . '.' . $model->rate[1];
Also, your if condition should look like the following (note the number of equal signs):
if ( strlen($model->rate) == 2 )

Related

How would you be able to check if there are at least 4 letters after a string in PHP

Currently I have it setup that if a user enters an invite, it will check if it starts with the right url. But how would you be able to check if there are at least 4 characters after the url?
url should like like this: https://discord.gg/AB1C...
My php (laravel) code that checks if the invite begins with the right string
$inviteok = substr($request->invite, 0, 19 ) === "https://discord.gg/";
$inviteok = substr($request->invite, 19, 24 ) == // validation?
The Str::after method returns everything after the given value in a string. The entire string will be returned if the value does not exist within the string:
use Illuminate\Support\Str;
$slice = Str::after('This is my name', 'This is');
// ' my name'
The Str::length method returns the length of the given string:
use Illuminate\Support\Str;
$length = Str::length('Laravel');
// 7
Then you can do something like
$lettersAfterUrl = Str::after($completeUrl, 'https://discord.gg');
$numLettersAferUrl = Str::length($lettersAfterUrl);
How about using preg_match() to parse entire URL in one call?
$isValid = (bool) preg_match('#^https:\/\/discord\.gg\/[A-Z0-9]{4,}$#', $request->invite);
I'm assuming you are looking for A-Z and 0-9 characters only.
If you use parse_url(), this splits up the URL into the parts and you can check them individually...
$invite = "https://discord.gg/AB1";
$parts = parse_url($invite);
$inviteok = $parts['host'] == 'discord.gg';
$inviteok = strlen($parts['path']) > 4;
note that $parts['path'] will be something like /AB1C, so account for the length being > 4.
Just use strlen
$urlok = substr($request->invite, 0, 19 ) === "https://discord.gg/";
if($urlok && strlen($request->invite) > 23) {
// do your thing here
}

How to generate numbers list starting from 3

I have this PHP code
What this code do is matching month count to numbers .
If condition is true echo result!
Result is displayed in table for each user.
$isT is count of months between 2 dates example 1,2,3 or 9
if($isT=='3'
OR $isT=='6'
OR $isT=='9'
OR $isT=='12'
OR $isT=='15'
OR $isT=='18'
OR $isT=='21'){
//echo something
}
What i want is make this numbers automatically generated
So it would be like:
if($isT==$generatednumber[$i]){
//echo something
}
i need numbers in this order 3 6 9 12 15. ..
basically +3 to the last number
First make sure its more than 0 then check if its multiple of 3.
if($isT >0 && ($isT % 3) == 0)){
//echo something
}
As i said modulus (%) is needed here:-
if(!empty($isT) && ($isT % 3) == 0)){
//echo something
}
Note:- this code will check:-
1.Your variable is set
2.Have some value
3.Is a multiple of 3.

Php If with || not working

do {
if ($dir[$i] == " " || is_numeric($dir[$i])
{
$direccion=$direccion.$dir[$i];
}
elseif ($i>6)
{
$i=strlen($dir)
}
}
while ($i <= count($dir))
$dir is the variable i use to get a google address, but the thing is that it comes with Zip code and City. The thing is that I only need the Street and number.
in the first run $dir = "Calle 30 433B29938 LaPlataBuenosAiresArgentina"
And i only need from that 30 433
Since the word " Calle " is always in the return, I've made an if that if its > 6, $i automatically will equal the lenght of the chain and kick me out of the while, because I'll be in "Calle " and there's when the 30 433 begins. The other ifs that I've made, one of them controls if It's a space to add it to the new string $direccion, and the other one checks if It's a number. If it's not a number and if It's not a space and $i > 6, means that I'm in B and I want to finish the do/while.
result of $direccion should be = 30 433. But i get an error on line 3 telling me that there is a problem with a {.
first thing you forget a ) at the end of your if condition
if ($dir[$i] == " " || is_numeric($dir[$i]))
and second if you want to get number out of a string you can use
preg_match_all('!\d+!', $str, $matches);
like if i use this code
$str = 'Calle 30 433B29938 LaPlataBuenosAiresArgentina ';
preg_match_all('!\d+!', $str, $matches);
output will be
Array ( [0] => Array ( [0] => 30 [1] => 433 [2] => 29938 ) )
But only if you are sure that your string will be exact you defined.
i am not sure but hope it can help.

How to check the first 2 characters ( case-insensitive ) in PHP?

I have a search input box.
I already check for the first 2 characters.
$first_two = substr($search,0,2);
They need to start with BS, so I check them like this :
if ($search AND $first_two == 'bs' ){
// ... do stuffs
}
After that, I just realize that I don't want to have any restriction on them.
I want my first two character is case-insensitive.
I want to allow BS, Bs, bs and bS.
What do I need to fix in my if (?) to allow these to happens ?
Use a certain case for your comparison, this example uses lower case
$first_two = substr($search,0,2);
if ($search AND strtolower($first_two) == 'bs' ){
// ... do stuffs
}
This way BS, Bs, bS will all be converted to bs and your comparison will always work
Use strcasecmp() like this:
if ($search AND strcasecmp($first_two , "bs") == 0)
//^If both strings are the same (case-insensitive) the function returns 0

How to convert some character into numeric in php?

I need help to change a character in php.
I got some code from the web:
char dest='a';
int conv=(int)dest;
Can I use this code to convert a character into numeric? Or do you have any ideas?
I just want to show the result as a decimal number:
if null == 0
if A == 1
Use ord() to return the ascii value. Subtract 96 to return a number where a=1, b=2....
Upper and lower case letters have different ASCII values, so if you want to handle them the same, you can use strtolower() to convert upper case to lower case.
To handle the NULL case, simply use if($dest). This will be true if $dest is something other than NULL or 0.
PHP is a loosely typed language, so there is no need to declare the types. So char dest='a'; is incorrect. Variables have $ prefix in PHP and no type declaration, so it should be $dest = 'a';.
Live Example
<?php
function toNumber($dest)
{
if ($dest)
return ord(strtolower($dest)) - 96;
else
return 0;
}
// Let's test the function...
echo toNumber(NULL) . " ";
echo toNumber('a') . " ";
echo toNumber('B') . " ";
echo toNumber('c');
// Output is:
// 0 1 2 3
?>
PS:
You can look at the ASCII values here.
It does indeed work as in the sample, except that you should be using php syntax (and as a sidenote: the language that code you found most probably was, it did not do the same thing).
So:
$in = "123";
$out = (int)$in;
Afterwards the following will be true:
$out === 123
This may help you:
http://www.php.net/manual/en/function.ord.php
So, if you need the ASCII code you will need to do:
$dest = 'a';
$conv = ord($dest);
If you want something like:
a == 1
b == 2
.
.
.
you should do:
$dest = 'a';
$conv = ord($dest)-96;
For more info on the ASCII codes: http://www.asciitable.com/
And for the function ord: http://www.php.net/manual/en/function.ord.php
It's very hard to answer because it's not a real question but just a little bit of it.
But if you ask.
It seems you need some translation table, that defines links between letters and numbers
A -> 2
B -> 3
C -> 4
S -> 1
or whatever.
You can achieve this by using an array, where keys would be these letters and values - desired numbers.
$defects_arr = array(
'A' -> 2,
'B' -> 3,
'C' -> 4'
'S' -> 1
};
Thus, you can convert these letters to numbers
$letter = 'A';
$number = $defects_arr($letter);
echo $number; // outputs 1
But it still seems is not what you want.
Do these defect types have any verbose equivalents? If so, why not to use them instead of letters?
Telling the whole story instead of little bit of it will help you to avoid mistakes and will save a ton of time, both yours and those who to answer.
Out of this question, if you are looking for convert RT0005 to 5
$max = 'RT0005';
return base_convert($max,10,10);
// return 5

Categories