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];
Related
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, "/"));
I have searched everywhere but can't find a solution that works for me.
I have the following:
$bedroom_array = array($studio, $one_bed, $two_bed, $three_bed, $four_bed);
For this example lets say:
$studio = '1';
$one_bed = '3';
$two_bed = '3';
I then use the implode function to put a comma in between all the values:
$bedroom_list = implode(", ", array_filter($bedroom_array));
echo $bedroom_list;
This then outputs:
1, 2, 3
What I want to do is find the last comma in the string and replace it with an &, so it would read:
1, 2 & 3
The string will not always be this long, it can be shorter or longer, e.g. 1, 2, 3, 4 and so on. I have looked into using substr but am not sure if this will work for what I need?
Pop off the last element, implode the rest together then stick the last one back on.
$bedroom_array = array('studio', 'one_bed', 'two_bed', 'three_bed', 'four_bed');
$last = array_pop($bedroom_array);
$string = count($bedroom_array) ? implode(", ", $bedroom_array) . " & " . $last : $last;
Convert & to the entity & if necessary.
if you have comma separated list of words you may use:
$keyword = "hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf, sdgdfg";
$keyword = preg_replace('/,([^,]*)$/', ' & \1', $keyword);
echo $keyword;
it will output:
hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf & sdgdfg
A one-liner alternative, that will work for any size array ($b = $bedroom_array):
echo count($b) <= 1 ? reset($b) : join(', ', array_slice($b, 0, -1)) . " & " . end($b);
strrpos finds the last occurrance of a specified string.
$str = '1, 2, 3';
$index = strrpos( $str, ',' );
if( $index !== FALSE )
$str[ $index ] = '&';
function fancy_implode($arr){
array_push($arr, implode(' and ', array_splice($arr, -2)));
return implode(', ', $arr);
}
I find this easier to read/understand and use
Does not modify the original array
Does not use regular expressions as those may fail if strings in the array contain commas, there could be a valid reason for that, something like this: array('Shirts (S, M, L)', 'Pants (72 x 37, 72 x 39)');
Delimiters don't have to be of the same length as with some of the other solutions
$bedroom_list = implode(", ", array_filter($bedroom_array));
$vars = $bedroom_list;
$last = strrchr($vars,",");
$last_ = str_replace(",","&",$last);
echo str_replace("$last","$last_",$vars);
<?php
$string = "3, 4, 5";
echo $string = preg_replace('/,( \d)$/', ' &\1', $string);
?>
Here's another way to do the replacement with a regular expression using a positive lookahead which doesn't require a backreference:
$bedroom_list = preg_replace('/,(?=[^,]*$)/',' &', implode(', ', $bedroom_array));
$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
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 ;