I have an MD5 alpha-numeric. How can I get the first five characters and last five characters, and put them in one string using PHP?
For example: "aabbccddeeffgghh" will become "aabbcfgghh".
First of all - accept your recent answers.
You can do this with substr function:
$input = 'aabbccddeeffgghh';
$output = substr($input, 0, 5) . substr($input, -5);
$extract = substr($input,0,5) . substr($input,-5);
Use substr - http://php.net/manual/en/function.substr.php
$hash = "aabbccddeeffgghh";
$tenChars = substr( $hash, 0, 5) . substr( $hash, -5 ); // "aabbcfgghh"
You want to use the substr function:
$md5hash = '098f6bcd4621d373cade4e832627b4f6';
$front_and_back = substr($md5hash,0,5) . substr($md5hash,-5);
$string = "aabbccddeeffgghh";
$string1 = substr($string,0,5);
$string2 = substr($string,-5);
$string = $string1 . $string2 ;
Related
I have a string 18-04, I want to make it 04-18.
I have tried doing this :
$myStr = "18-04";
$first = substr($myStr, 0, 1);
$second = substr($myStr, 3, 4);
$final = $second . '-' . $first;
But I was looking for a simpler way rather than writing 5 full lines to do something simple. Any idea?
Use regex in preg_replace() to do this work.
$final = preg_replace("/(\d+)-(\d+)/", "$2-$1", $myStr);
Check result in demo
Is it shorter ? :)
echo implode('-',array_reverse(explode('-',$myStr)));
you can use the following:
$myStr = "18-04";
$chuncks = explode("-",$myStr);
$final = $chuncks[1]. '-' . $chuncks[0];
I have string like this
Ameerpet,|Jeans Corner: 040-50607090#05:45PM/6
want to get the substring after # and before /.
Tried the following
echo substr($str,strpos($str,'#')+1,strpos($str,'/'))
But i will get the whole string after #
Output 05:45PM/6
You may use preg_match
$match = preg_match('~#\K[^/]*(?=/)~', $str);
DEMO
Try This,
echo substr($str,
strpos($str,'#')+1,
strpos($str,'/') - strpos($str,'#') - 1);
The third parameter is not < position > , its < length >
refer this : http://php.net/manual/en/function.substr.php
Syntax of substr is not substr(str , start ,end)
The correct syntax is string substr ( string $string , int $start [, int $length ] )
This is the reason why entire thing is getting printed to achieve the solution you can use the solution provided by #Avinash Raj . However you are trying to get time which is of length 7 then you can use the syntax in this manner
echo substr($str,strpos($str,'#')+1,7)
Thanks
Try this code:
$data = "Ameerpet,|Jeans Corner: 040-50607090#05:45PM/6";
$first = substr($data, strpos($data, "#") + 1);
$splitter = "/";
$pieces = explode($splitter, $first );
echo $pieces[0];
You may do it with substr also
$yourString="Ameerpet,|Jeans Corner: 040-50607090#05:45PM/6";
$valueAfterSpecialChar = substr($yourString, strpos($yourString, "#") + 1);
echo $desiredOutput= substr($valueAfterSpecialChar, 0, strpos($valueAfterSpecialChar, "/"));
How would I extract the number 33 before the underscore in the following?
33_restoffilename.txt.
would something like following work?
int strPos = strpos("33_filename.txt", "_");
str num = substr ("33_filename.txt" , 0 , strPos );
there are may way to achive this:
Method 1:
$strPos = strpos("33_filename.txt", "_");
echo $num = substr("33_filename.txt" , 0 , $strPos );
Method 2:
$str = '33_filename.txt';
$str_arr = explode('_', $str);
echo $num = $str_arr[0];
If the naming convention is always number_filename.ext, you can use explode():
//Put the filename into the variable $name
$name = "33_restoffilename.txt";
//Split the name by "_"
$parts = explode("_", $name);
//Get the first part of the name from the array (position 1)
$number = $parts[0];
//Output
echo $number;
This will output
33
Use strstr().
$str = '33_filename.txt';
$num = strstr($str, '_', true);
Method - 1
$getIntegerFromBeginning = substr($string, 0, strspn($string, "0123456789"));
echo $getIntegerFromBeginning;
Method - 2
$getIntegerFromBeginning = explode("_", $string, 2);
echo $getIntegerFromBeginning[0];
Replace $string with exact string.
Use this
$string = '33_filename.txt';
$arrString = explode('_', $string);
echo $value = $arrString[0]; //Will output 33
This is really very simple we don't need these string functions in this case. Just do type casting and you will get integer number. It will be fast and easy
$pp='33_restoffilename.txt';
echo (int)$pp;
No need to make it complicated
$var = "Hello";
How can I insert the character : in after the first character of the string above ? The first character could be anything. So I want to do something like
$res = add("Hello");
echo $res; // which return H:ello
I know how to do this through str_replace but this needs the first character to be always the same..
Any help would be highly appreciated
you can use string append and substr to do it
$var = $var[0].":".substr($var,1);
$str = substr($str, 0, 1) . ':' . substr($str, 1)
$newstr = substr_replace($oldstr, $str_to_insert, $pos, 0);
http://www.php.net/manual/en/function.substr-replace.php
<?php
function addColon($myString){
return $myString = substr($myString, 0, 1) . ':' . substr($myString, 1);
}
echo addColon("Hello");
?>
In PHP I would probably try this:
$string = "Hello";
$new_string = substr($string,0,1) . ":" . substr($string,1, strlen($string)-1);
This would get H (= first char) + : (= fixed char) + ello (= the rest of the string)
how to convert string sample 1 to sample 2 in PHP:
this string : 0510
after : 05:10
thanks
Without giving more info, there are hundreds of ways to convert '0510' to '05:10'. You can use .substr():
$string = '0510';
$string = substr($string, 0, 2).':'.substr($string, -2);
Or brackets:
$string = $string[0].$string[1].':'.$string[2].$string[3];
Or .str_split() with .implode():
$string = implode(':', str_split($string, 2));
Or .preg_replace():
$string = preg_replace(`~(\d{2})(\d{2})~`, '$1:$2', $string);
I'm not really sure about what you want but maybe you're looking for substr
$sample2 = substr($sample1, 0, 2) . ':' . substr($sample1, -2);
See http://php.net/manual/en/function.substr.php for more information.
As you added the "clock" tag, I assume you are talking about times. So you can use some date/time functions for this as well:
echo date("H:i", strtotime('0510'));
Output:
05:10
Use substr() to split the string into two and append them again.
$string = '0510';
$first = substr($string, 0, 2);
$second = substr($string, 2);
echo $first . ':' . $second