How is strpos() in PHP counting? [duplicate] - php

This question already has answers here:
Safe to use strpos with UTF-8 strings?
(2 answers)
mb_strpos vs strpos, what's the difference?
(3 answers)
Closed 5 years ago.
I see some strange results with strpos() function...
echo strpos("naše republika", "a"); // 1
echo strpos("naše republika", "š"); // 2
echo strpos("naše republika", "e"); // 4 ???
Can someone explain it to me? Thanks. (php 5.6.30)

Related

PHP issue:Why is there blank output while running this code?And Why is there a 1 in the other case? [duplicate]

This question already has answers here:
PHP printed boolean value is empty, why?
(4 answers)
Why doesn't PHP print TRUE/FALSE? [duplicate]
(3 answers)
Closed 3 years ago.
Using == vs Using !=
/*Testing some basic scenarios in PHP*/
$a=7;
$b=5;
echo $a==$b;
$a=7;
$b=5;
echo $a!=$b;
Output is blank screen in case 1.
Output is 1 in case 2
.Why am I getting these respective outputs?

How to use Substr in php [duplicate]

This question already has answers here:
How to convert JavaScript code to its PHP equivalent?
(1 answer)
UTF-8 all the way through
(13 answers)
Closed 5 years ago.
I have php code:
$b = 'aHR0cDovL3d3dy5oZHpvZy5jb20vZ2V0X2ZpbGUvМS84Y2Е5МTЕ4ZmМyNmVkNTk0ZmI5Yzc2ZWI2Y2Y2YWVmМС85NDАwМС85NDU4Ny85NDU4Ny5tcDQvP3RpbWU9МjАxNzА5МjYyМDIxNDYmcz05МTUzZmNmYjАyOTUyOWQxY2JhZTВkYzNkY2ZhODVmZiZicj0xODЕ1JmQ9МTcwNyZmPXZpZGVvLm0zdTg~';
echo substr($b,40,1);
But it not print M charater. It show '�' . Why?

PHP return 1 number after dot in float [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 6 years ago.
I have got this php code
$Likes=1112;
$Likes=$Likes/1000;
echo $Likes."k";
This code returns me 1.112k,but my goal is to get 1.1k
Use the number_format function:
echo number_format($Likes,1)."k";
And a coding style advice: don't start your variables with an upper case letter!

MYSQL Trim function using on PHP [duplicate]

This question already has answers here:
Php trim string at a particular character
(7 answers)
Closed 8 years ago.
I have a mysql codes which i need to do that trim and leading with PHP, anyone know how to write below logics with PHP code.
LEFT(TRIM(LEADING 'DHL ' FROM CONT_NAAM),20)
TRIM(LEADING 'DHL JVGL' FROM CONT_NAAM)
You can use preg_replace:
substr(preg_replace('/^(DHL )+/', '', $cont_naam), 1, 20)

How to find 3 or more of a character in a string? [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Closed 9 years ago.
$string = "oidjdssd , odi,jdois, 3089u,, oisdjsd";
How do i find out if theres more than 3 commas in the string above in the best way?
I would suggest substr_count. You can see if the result is >3 to see if there's more than three.
echo count_chars($string)[ord(',')];
Or for PHP<5.4
$chars = count_chars($string);
echo $chars[ord(',')];
BTW: As it seems, that you are handling CSV-data, you should have a look at str_getcsv()

Categories