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
Related
This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);
This question already has answers here:
removing #email.com from string in php
(6 answers)
Closed 4 years ago.
How can I use the str_replace() without using an array of words for turning:
some#mail.com
into
some
So, everything after the '#' sign includes # as well.
How can I do that?
As an example, i will type 'adminofsite#xxxwwweeerandomstuff.com'
and the output will be: 'adminofsite'.
use strstr
$email="john#doe.com"
$user = strstr($email, '#', true);
echo $user;
$str = "some#mail.com"
$str = substr($str,0,strpos($str,"#"))
function stripEmailDomain($string){
return substr($string, 0, strpos($string, '#'));
}
I exploded and then rebuilt the string. This will work if you are positive the string will always be an email address. Its a work around but seems flexible if you want to modify it for a specific purpose.
<?php
$explo0= explode('#',"some#mail.com");
for($i=0;$i<count($explo0);$i++){
$exploresult0.=$explo0[$i+1];
}
echo "#".$exploresult0;
?>
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];
?>
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 8 years ago.
$string = "comment-454";
I want to disply only 454? not comment-
i am saving in db comment-454 , now i want to crop comment-.. how can i
can someone provide me something, because i am newbie in php..
thanks in advance..
$string = str_replace("comment-", "", "comment-454");
should replace what you want.
you mentioned comment-454 came from your database. So ill assume it's in $result.
Now you can do:
$string = str_replace("comment-", "", $result);
echo $string;
Use explote() http://php.net/manual/de/function.explode.php
$teile = explode("-", $string);
echo $teile[1]; // 454
This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);