How to remove 4th letter in string using PHP ?
I use this code.
<?php
$str = "1234567890";
$str2 = mb_substr($str, 4);
echo $str2;
?>
But it's will echo 567890
I want to echo 123567890 remove 4 from string.
How can i do ?
You can try substr_replace for this. Here we are replacing 4 which is at 3rd index.
Try this code snippet here
<?php
$str = "1234567890";
echo substr_replace($str, "", 3,1);
try setting the 3rd index to null
<?php
$str = "1234567890";
$str[3] = null;
echo $str;
try with below sulution:
$str = '1234567890';
$str_arr = str_split($str);
unset($str_arr[3]);
echo implode('', $str_arr);
output:
123567890
There are multiple ways of performing any operations on string variables in php
// can be used for printing purpose
$str = "1234567890";
echo substr($str,0,3).substr($str,4);
// actual replacement of string
$str = "1234567890";
echo substr_replace($str, "", 3,1);
Related
i have one string
$str ='california 94063';
now i want california and 94063 both in diferent variable.
string can be anything
Thanks in advance....
How about
$strings = explode(' ', $str);
Assuming that your string has ' ' as a separator.
Then, if you want to find the numeric entries of the $strings array, you can use is_numeric function.
Do like this
list($str1,$str2)=explode(' ',$str);
echo $str2;
If your string layout is always the same (say: follows a given format) then I'd use sscanf (http://www.php.net/manual/en/function.sscanf.php).
list($str, $number) = sscanf('california 94063, "%str %d");
<?php
$str ='california 94063';
$x = preg_match('(([a-zA-Z]*) ([0-9]*))',$str, $r);
echo 'String Part='. $r[1];
echo "<br />";
echo 'Number Part='.$r[2];
?>
If text pattern can be changed then I found this solution
Source ::
How to separate letters and digits from a string in php
<?php
$string="94063 california";
$chars = '';
$nums = '';
for ($index=0;$index<strlen($string);$index++) {
if(isNumber($string[$index]))
$nums .= $string[$index];
else
$chars .= $string[$index];
}
echo "Chars: -".trim($chars)."-<br>Nums: -".trim($nums)."-";
function isNumber($c) {
return preg_match('/[0-9]/', $c);
}
?>
I am trying out a Logic in strings but facing difficulties in string manipulation functions. Which function will be good for this below approach:
My String is "Hello" I want to add "------------------" after the first string that is "Hello--------------" and the length of the string should be 20 after the string manipulation.
I want to add "------------------" to the string to make it 20 length.
In other words: Hello+Underscores
If the string length is too much we can trim the string.
Below is the code which I tried.
<?php
$challenge = 'hello';
$length = strlen($challenge);
$i= $length +1;
$challenge=substr($challenge,0,$i);
echo $challenge.'<br>';
?>
I tried string concatenation but I am sure I cant use it in this logic, I think the string adding should be done with preg_replace.
Can some one give a good advice on it!
str-pad is the easiest way to achieve your task and code sample as follows.
<?php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
echo str_pad($input, 6 , "___"); // produces "Alien_"
?>
Just use str_pad.
$input = 'hello';
$output = str_pad($input, 20, '_');
echo $output;
demo: http://ideone.com/0EPoV2
Here you go
<?php
$string = "anything";
echo substr($string."------------------------------------------",0,20);
?>
Just use the first 20 chars of your string and ------------------------
Edit based on new requirement not given in original question for some reason.
<?php
$string = "anything";
$newstring = substr($string."------------------------------------------",0,20);
echo $newstring."whatever you want to add at end";
?>
Try this
<?php
$input = "HELLO";
echo str_pad($input, 10, "----", STR_PAD_RIGHT);
?>
here $input is string and 10 is length of char added STR_PAD_RIGHT is position
View this link PHP.net
$str = 'Hello';
$str .= "_";
while(strlen($str) <= 20){
$str .= "-";
}
echo $str;
try this code
$challenge = 'hello';
$length = strlen($challenge);
if($length < 20){
$limit = 20-$length;
for($i=0;$i<$limit;$i++){
$challenge .= '_';
}
}
echo $challenge;
How can I extract 4 from this string?
$string = "Rank_1:1:4";
I'm trying to get pagerank from Googles server, and the last value (4) is the actual pagerank.
Try
$string = "Rank_1:1:4";
$data = explode(':',$string);
echo end($data);
EDIT
as per #MichaelHampton, if they add more fields later, then use as below
$string = "Rank_1:1:4";
$data = explode(':',$string);
echo $data[2];
PHP has so many string function you can use ...
Variables
$find = ":";
$string = "Rank_1:1:4";
Using substr
echo substr($string, strrpos($string, $find) + 1);
Using strrchr
echo ltrim(strrchr($string, $find),$find);
$pattern = '/:\d+$/';
preg_match($pattern, $string, $matches);
$rank = substr($matches[0],1);
In PHP, how can you replace the second and third character of a string with an X so string would become sXXing?
The string's length would be fixed at six characters.
Thanks
It depends on what you are doing.
In most cases, you will use :
$string = "string";
$string[1] = "X";
$string[2] = "X";
This will sets $string to "sXXing", as well as
substr_replace('string', 'XX', 1, 2);
But if you want a prefect way to do such a cut, you should be aware of encodings.
If your $string is 我很喜欢重庆, your output will be "�XX很喜欢" instead of "我XX欢重庆".
A "perfect" way to avoid encoding problems is to use the PHP MultiByte String extension.
And a custom mb_substr_replace because it has not been already implemented :
function mb_substr_replace($output, $replace, $posOpen, $posClose) {
return mb_substr($output, 0, $posOpen) . $replace . mb_substr($output, $posClose + 1);
}
Then, code :
echo mb_substr_replace('我很喜欢重庆', 'XX', 1, 2);
will show you 我XX欢重庆.
Simple:
<?php
$str = "string";
$str[1] = $str[2] = "X";
echo $str;
?>
For replacing, use function
$str = 'bar';
$str[1] = 'A';
echo $str; // prints bAr
or you could use the library function substr_replace as:
$str = substr_replace($str,$char,$pos,1);
similarly for 3rd position
function mb_substr_replace($string, $replacement, $start, $length=0)
{
return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start+$length);
}
same as above, but standardized to be more like substr_replace (-substr- functions usually take length, not end position)
I am trying to trim a string in PHP so that I can only get certain text from the String.
I have an email stored to a String for instance some_name#somedomain.com .
How can I remove the text after the '#' so that I would only 'some_name'?
In PHP you can do :
$string = 'some_name#somedomain.com';
$res = explode('#', $string);
echo $res[0];
Or you can use regexp, string functions in php ... etc
You should know both ways to do this:
substr
$mail = "some_name#somedomain.com";
echo substr($mail, 0, strpos($mail, '#') );
explode
list($name, $domain) = explode('#', $mail);
echo $name;
If you don't need the $domain you can skip it:
list($name) = explode('#', $mail);
More about list.
Demo: http://ideone.com/lbvQF
$str = 'some_name#somedomain.com';
$strpos = strpos($str, "#");
echo $email = substr($str, 0,$strpos);
you can try this to get string before #
Try This
$str1 = "Hello World";
echo trim($str1,"World");
You could try split using regex and the # symbol. This will return two Strings which you can then use just to acquire the 'some_name'.
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
String s = "some_name#somedomain.com";
String name = s.substring(0,s.indexOf("#");