Trim Part of a string in php [duplicate] - php

This question already has answers here:
How to extract only part of string in PHP?
(3 answers)
Closed 5 years ago.
I have a string in a variable like this:
$var = "This is a banana (fruit)";
How do I trim / get the part 'fruit' only? (i.e., data inside braces whatever it is).

Try this:
<?php
$sentence = "This is a banana (fruit)";
$a = stripos($sentence,"(");
$b = stripos($sentence,")");
echo substr($sentence, $a, $b);
?>

Use preg_rpelace function:
<?php
$str = "This is a banana (fruit)";
echo preg_replace('/.*\((.*)\).*/','$1',$str);
?>

Related

How to use substr() on php? [duplicate]

This question already has answers here:
How substr function works? [closed]
(3 answers)
Closed 4 years ago.
I am trying to get "pa" from "a(bcdefghijkl(mno)pa)q".
This is my code for exampe:
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,15);
echo $mystring;
outputs is:
mno)pa)q
You have use right code but the second parameter is wrong. use this one below
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,2);
echo $mystring;
In substr function:
first parameter means from which position of string starts.
and the second parameter means how many characters you have want.
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,-4,2);
echo $mystring;
Try this

Array out of text in PHP [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 5 years ago.
So let's say I have a String that looks like this: "apple, banana, orange". How do i split the text to get a Array that looks like this "apple", "banana", "orange".
Thanks in advance.
Use the explode PHP function :)
<?php
$str = "apple, banana, orange";
print_r (explode(" ",$str));
?>

PHP String Split/Explode [duplicate]

This question already has answers here:
How to limit the elements created by explode()
(4 answers)
Closed 7 months ago.
I have a String "Hello I am apple"
I want to split to two string
str1 = "Hello"
str2 = "I am apple"
so just simply split by the first space, then the following words will be in one string
Explode function has a 3rd parameter for that:
limit
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
http://php.net/explode
list($str1, $str2) = explode(" ", $str, 2);
Same result also can be achieved by following code....
<?php
$str = "Hello I am Apple";
$str_con = explode(" ",$str,2);
echo $str_con[0];
echo $str_con[1];
?>

How to make the first letter of a word uppercase? [duplicate]

This question already has answers here:
Uppercase first letter and rest lower
(6 answers)
Closed 7 years ago.
I have this word:
katii
But I want the first character in uppercase K. How can I do this?
Use function usfirst
string ucfirst ( string $str )
You can use below as prescribed by php.net
<?php
$foo = 'hello world!';
$foo = ucfirst($foo); // Hello world!
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>
$upper = strtoupper(substr('katii', 0, 1)); //K
You should make your question a little clearer in future, though.

converting from a string to a variable in php [duplicate]

This question already has answers here:
How to expand variables in a string
(7 answers)
Closed 8 years ago.
I have written this,
<?php
function get_pounds($var){
$string ='i have $var pounds';
return $string;
}
$v =100;
echo get_pounds($v);
?>
outputs:
i have $var pounds
I know I can use "" or $string ='i have'. $var .'pounds'; to avoid this problem. But take a situation when fetching string from a database. I want it to parse it and make $var as a variable on its own without being part of the string.
say
variable
I have $var pounds
My query goes "select variable from $table;"
return mysql_query($query);
This will return a string.
How do I parse it so that my $var is not parsed as a var ?
instead of $string ='i have $var pounds'; try $string ="i have $var pounds";

Categories