This question already has answers here:
php trim(), work poorly
(2 answers)
Closed 9 years ago.
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "public://"));
Result -: Stock_000000527255XSmall.jpg
Why the missing i? But when my character starts with si, I get si in the result. Why does trim behave differently?
$individual_file["uri"] = "public://siStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "public://"));
Result -: siStock_000000527255XSmall.jpg
It's because charlist is literally a list of single characters to remove from the left side of the string and i is listed in public://. Any character that falls in this list will be removed, no matter the order.
Ref: http://php.net/manual/en/function.ltrim.php
In fact this:
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "publc://"));
would output:
ic://iStock_000000527255XSmall.jpg
Another example by changing the order:
$individual_file["uri"] = "public://iStock_000000527255XSmall.jpg";
print_r(ltrim($individual_file["uri"], "bcilpu:/"));
would output:
Stock_000000527255XSmall.jpg
Related
This question already has answers here:
Getting the first character of a string with $str[0]
(9 answers)
Closed 2 years ago.
I'm using the following code to display only the last name first character on my Wordpress site:
$name = $comment->comment_author;
$separate = mb_split(" ", $name);
$last = array_pop($separate);
echo implode(' ', $separate)." ".$last[0].".";
It works great with English names, but $last[0] will return a question mark when used with a foreign language (e.g Arabic, Hebrew, Greek etc). For example:
Name: השם שלי
Will return:
השם ?.
I've been trying to fix this for an hour now, but nothing so far.
Any idea?
You can try to set the encoding (https://www.php.net/manual/ro/function.mb-internal-encoding.php) before using the mb_split function.
This question already has answers here:
preg_replace: add number after backreference
(2 answers)
Closed 6 years ago.
I referred this question - Capture group reference + numeral
I tried with both the options provided there. Here is example code using ECMAScript expression (I guess I need to use this, correct me if I am wrong) -
$string = "box.last_rollout_revision = dummy";
$regex = "/(box\.last_rollout_revision[\s]*=[\s]*)[a-z0-9-_]*([\n]*)/";
$test = "234";
$replacementPattern = "$1".$test."$2";
echo preg_replace($regex,$replacementPattern,$string);
Expected output
box.last_rollout_revision = 234
Actual getting
34
P.S. I am trying to replace value against a key in a config file which has several other key-value pairs. I think regex will make it easier in this case. Correct me if I am wrong.
Change the replacement pattern to
$replacementPattern = '${1}'.$test.'$2';
See the IDEONE demo
This question already has answers here:
Php length of cyrillic string doubles its value
(3 answers)
Closed 7 years ago.
I'm doing application for string length check. When checking Cyrillic string there shows wrong because of the unicode. How to solve this problem?
$str=strlen ('abc');
echo $str; // result is 3
$str=strlen ('АБС');
echo $str; // result is 6. How to find correct value
Was little curious about this question.
First options is as suggested by Rizier123(no doubt, that works perfectly.)
Second:
You can also use utf8_decode() to get the result.
<?php
$str=strlen (utf8_decode('АБС'));
echo $str;
?>
Check demo here
This question already has answers here:
Truncate a string to first n characters of a string and add three dots if any characters are removed
(20 answers)
Closed 7 years ago.
I saw this post which had my ans: MySQL Select Query - Get only first 10 characters of a value
How to do the same using php ?
This can be done by substr function. Following is the working example with code.
http://sugunan.net/demo/substr.php
<?php
$full_str = "Hello, this is my subject and how are you";
$firstn = substr ( $full_str , 0 ,10 );
echo $firstn;
?>
Use the substr() function. Read about it here http://php.net/manual/en/function.substr.php
$shortString = substr($longString,$startPoint,$numberOfCharactersToReturn);
eg: substr('abcdefg',0,3);
// returns 'abc'
This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 7 years ago.
I have a string like this in the database
!80!n!98!n!22!n!6!n!76!n!1!n!24!n!129!n!59!n!73!n!7!n!40!n!85!n!35!n!42!n!126!n!81!n!37!n!128!n!147!n!106!n
I want to remove those ! and n symbols and want to add a pipe in between characters
80|98|22|6|76|1|24|129|59|73|7|40|85|35|42|126|81|37|128|147|106
you could update the column first:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, '!n!', '|')
WHERE SomeOtherColumn LIKE '%PATTERN%'
More information on replace here:
How can I use mySQL replace() to replace strings in multiple records?
<?php
$str="!80!n!98!n!22!n!6!n!76!n!1!n!24!n!129!n!59!n!73!n!7!n!40!n!85!n!35!n!42!n!126!n!81!n!37!n!128!n!147!n!106!n
";
$str1 = preg_replace('/\s+/','|',str_replace(array("!n","!","!n!"),' ',trim($str)));
echo $output = substr($str1, 1, -1);
OUTPUT
80|98|22|6|76|1|24|129|59|73|7|40|85|35|42|126|81|37|128|147|106