How to use SQL TRIM Function in Laravel [duplicate] - php

This question already has answers here:
How to perform TRIM() and CONCAT() in Laravel Query Builder?
(4 answers)
Closed 3 years ago.
I'm working on a school project that register and login with "username", as far as i tested i can't find any good method to work, I've found this function of SQL TRIM which removes spaces and other character if anyone knows how to work with it in laravel.
reference:
http://www.sqltutorial.org/sql-string-functions/sql-trim/

You can use this Example:
$data = DB::table('table_name')
->select(
DB::raw("TRIM(CONCAT(field1,' ',field2,' ',field3)) AS Name")
)->get();

Related

How do I access this string [duplicate]

This question already has answers here:
How to use php serialize() and unserialize()
(10 answers)
Closed 1 year ago.
I just added some custom fields for users. So 'api' is one of them.
function abc(){
$current_user = wp_get_current_user();
print_r(get_user_meta($current_user->id)['api'][0]);
}
add_action('init', 'abc');
And the result I get is a string:
a:2:{i:0;s:9:"paybphone";i:1;s:3:"ips";}
I don't know how to access "i" value from the PHP code as its string. And I also know its not JSON , so I also can't use 'json_encode()' function. Please help me out ! I am new to Wordpress programming.
You can pass your result into the unserialize() function from php, it will return your PHP array that you can easily access after
More info : https://www.php.net/manual/en/function.unserialize.php

How would I take a string, find a certain character, add a character, and continue until end of string? [duplicate]

This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you

What does these ... mean in this example? [duplicate]

This question already has answers here:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass

can I create a function without brackets? [duplicate]

This question already has answers here:
Can I create a PHP function that I can call without parentheses?
(6 answers)
Closed 6 years ago.
all php functions need () in the end. However, exit doesnt need that.
Can I create a function manually, which I can later execute without () ?
Even more, If I have full access to php installation?
p.s. please dont tell me answers "exit is not function" or etc (My question is not if "exit" is function or not). I want to know HOW TO ACHIEVE like that.
No you can't. You have to edit Base of PHP language to accomplish this.
exit , echo , print and etc are not function .

Dynamic prefix attribute PHP [duplicate]

This question already has an answer here:
How can I access a property with an invalid name?
(1 answer)
Closed 6 years ago.
I have a question about attributes in PHP.
I have various Class with the same attributes, but with with different prefix.
Example:
$attr->a_field;
$attr2->b_field;
So, with another Class I want to access to them.
I tried:
$field = "{$prefix}_field";
$attr->{$field}
and it works perfect. But is any other way to doing this?
I tried also with:
$attr->{$prefix}_field;
$attr->{$prefix}{"_field"};
$attr->"{$prefix}_field";
etc and who I suppose I get PHP's errors
Thanks!
You can write it directly as $attr->{"{$prefix}_field"}, as shown in the docs.
You're looking into variable variables
$attr->{$prefix."_field"}

Categories