PHP function substr() error
substr
is counting using bytes, and not characters.
greek probably means you are using some multi-byte encoding, like UTF-8 -- and counting per bytes is not quite good for those.
Maybe using mb_substr
could help, here : the mb_*
functions have been created specifically for multi-byte encodings.
PHP substr() function is not working as expected
I managed to solve the problem by using utf8_decode
, i.e.:
$amount = utf8_decode('€300');
echo substr($amount ,1);
//300
I have a problem with the strpos & substr function on PHP
The problem is that $find_and
is the index of ,
, but the third argument to substr()
needs to be the length of the substring, not the ending index. So
$find_email = substr($temp,$find_or+3,$find_and);
should be$find_email = substr($temp,$find_or+3,$find_and-$find_or-3);
For $find_passeord
you can omit the 3rd argument, since the default is the end of the string.However, this would be simpler with a regular expression:
if (preg_match('/^U:(.*?)\|E:(.*?),P:(.*)/', $temp, $match)) {
list($whole, $user, $email, $password) = $match;
}
php substr not working on variable
The
<%=flight.flightnum%>
is processed client side so your:echo substr($result, 0 ,3)
is putting just:<%=
into your source (e.g. the first 3 characters). This is breaking the page generation because the mark up is invalid.You will need to resolve this issue client side after what ever runs the <%=stuff%>
processing.
Additionally, var_dump
outputs information about a variable. It will display its value, type, and length. The 21 in this instance was the length of <%=flight.flightnum%>
.. this also was outputted but whatever is running the <%=
s (I thought ASP) converted it to the flight value.
Php substr Utf-8 issue
You should use multi-byte substr()
function.
Try
<?php
$string = '<p>Şelamiİnnşşasdüğ213,123wqeq.weqw.rqasd</p>p>Şelamiİnnşşasdüğ213,123wqeq.weqw.rqasd</p><p>Şelamiİnnşşasdüğ213,123wqeq.weqw.rqasd</p>';
echo mb_substr(strip_tags(trim(html_entity_decode($string, ENT_COMPAT, 'UTF-8'))), 0, 14);;
?>
Ref | Demo substr() return incorrect number of characters
You are using substr incorrectly.
You are using the last value passed to the function as an end point rather than a length, as in grab the characters between 25 and 50, instead of using substr as it supposed to be used, which, as you have written it, is grab 50 characters after skipping the first 25 characters.
The final function call should be substr($nature_goods, 25, 25); if you are expecting to receive 25 characters in return.
Values expected for substr:
substr ( string $string , int $start , int $length )
Full documentation here:
http://php.net/manual/en/function.substr.php
Not defined Substr in PHP
Typo error subtr()
. Its substr()
.
SUBSTR in laravel query builder returns an error
$items2 = Course::select(\DB::raw("SUBSTR(name, LOCATE('-', name) + 1) as name"))
->where('school_id','=',Auth::user()->school_id)
->where('status','=',1)
->get();
use select to get the specific column and don't forget to alias the column as name just to be sure. The query you were using fetch all the data from the database.
Related Topics
PHP Upload File Enhance Security
What Is the Js Equivalent to the PHP Function Number_Format
Make Script Execution to Unlimited
Best Way to Get Hostname with PHP
Creating and Invoking an Anonymous Function in a Single Statement
Mongodb: Benefit of Using Objectid VS a String Containing an Id
Should a Function Use: Return Null;
Can One Modify the Templates Created by Artisan Migrate Command
How to Use Laravel Passport with a Custom Username Column
Pdo Prepared Statements for Insert and on Duplicate Key Update with Named Placeholders
How to Increase by 1 All Keys in an Array
How to Sort Null Values Last Using Eloquent in Laravel
PHP - Display a 404 Error Without Redirecting to Another Page
Cannot Set Date.Timezone in PHP.Ini File
Can't Change Font Size for Gd Imagestring()
Randomize a PHP Array with a Seed