how to set a character betwen other characters in php - php

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

Related

Change position of strings separated by character using php

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];

How can I display all the characters after 20 characters in a string using PHP?

For example, in the string given below:
$string = "adadadadadadadadaadadadadadadadadadadadadadadadadadadadadadadadadadadadadaddadadadadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafewgewrehrejrejreerj";
I would like the output to consist of all the characters except the first 20.
How do I achieve this?
Here is the code:
$string = "adadadadadadadadaadadadadadadadadadadadadadadadadadadadadadadadadadadadadaddadadadadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafewgewrehrejrejreerj";
echo substr($string, 20)
And check output.
Here is the code,
echo substr($string, 20);
Documentation link of substr which states Return part of a string depends on your requirement.
If you need the right part of the string after the 20th character you could use substr
$string = "adadadadadadadadaadadadadadadadadadadadadadadadadadadadadadadadadadadadadaddadadadadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafewgewrehrejrejreerj";
$rest = substr($string, 20, -1);
see this for ref http://php.net/manual/en/function.substr.php
You can use the substr($string, $start)
You can see the manual here http://php.net/manual/en/function.substr.php
if you have to only print string then you can direct echo / print .
echo "your string";
$twentieth_char = substr($string, 19, 1);

Getting Middle of string in php

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, "/"));

insert character after first character in string?

$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)

extract first 5 and last 5 characters from a string using php?

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 ;

Categories