Getting Middle of string in php - 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, "/"));

Related

Extract a part of the string

I have the path as follows in my variable.
$str='./application/language/english\admin\settings_lang.php';
How can I extract the part of the string after english\? I need to get admin\settings_lang.php
You can use explode function
$str= './application/language/english\admin\settings_lang.php';
$result = explode('english',$str);
echo $result[1];
Use substr php substr function
$str='./application/language/english\admin\settings_lang.php';
$newStr= substr($str, strpos($str, '\\') + 1);
If the string is fixed you can just use the PHP function substr and count how many places you need. Example:
$str='./application/language/english\admin\settings_lang.php';
$newStr = substr($str, -23);
If it's not of fixed length, but you know "admin" or some other substring will always be in there, you can combine substr with strpos like this:
$str='./application/language/other_stuff/dynamic/who_knows/english\admin\settings_lang.php';
$newStr = substr($str, strpos($str, 'admin'));
You can see strpos here.
Hi try the following code,
UPDATE
$str='./application/language/english\admin\settings_lang.php';
$whatIWant = substr($str, strpos($str, "\\") + 1);
echo $whatIWant;
Now check

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

How to get all the characters between [] using Pregmatch php

I have a string
$str = "[xyz.hlp] into asasa jkljk [xyp.htq] zff [xrt.thg]";
I want to get the character from the string and make an array of all those characters . for example for the above string provided I shuould get and array like this
$array("xyz.hlp","xyp.htq","xrt.thg");
I tried using preg_match(); something like this but it didn't work
preg_match('/\[(.*)\]/', $str , $Fdesc);
Thanks in Advance
I got the desired output but by using loop and some php string functions
<?php
$str = "[xyz.hlp] into asasa jkljk [xyp.htq] zff [xrt.thg]";
$i = 0;
while ($i != strrpos($str, "]")) {
$f_pos = strpos($str, "[", $i); // for first position
$l_pos = strpos($str, "]", $f_pos + 1); // for the last position
$value = substr($str, $f_pos, ($l_pos - $f_pos) + 1);
echo $value;
$i = $l_pos;
}
?>

How to get everything after a certain character?

I've got a string and I'd like to get everything after a certain value. The string always starts off with a set of numbers and then an underscore. I'd like to get the rest of the string after the underscore. So for example if I have the following strings and what I'd like returned:
"123_String" -> "String"
"233718_This_is_a_string" -> "This_is_a_string"
"83_Another Example" -> "Another Example"
How can I go about doing something like this?
The strpos() finds the offset of the underscore, then substr grabs everything from that index plus 1, onwards.
$data = "123_String";
$whatIWant = substr($data, strpos($data, "_") + 1);
echo $whatIWant;
If you also want to check if the underscore character (_) exists in your string before trying to get it, you can use the following:
if (($pos = strpos($data, "_")) !== FALSE) {
$whatIWant = substr($data, $pos+1);
}
strtok is an overlooked function for this sort of thing. It is meant to be quite fast.
$s = '233718_This_is_a_string';
$firstPart = strtok( $s, '_' );
$allTheRest = strtok( '' );
Empty string like this will force the rest of the string to be returned.
NB if there was nothing at all after the '_' you would get a FALSE value for $allTheRest which, as stated in the documentation, must be tested with ===, to distinguish from other falsy values.
Here is the method by using explode:
$text = explode('_', '233718_This_is_a_string', 2)[1]; // Returns This_is_a_string
or:
$text = end((explode('_', '233718_This_is_a_string', 2)));
By specifying 2 for the limit parameter in explode(), it returns array with 2 maximum elements separated by the string delimiter. Returning 2nd element ([1]), will give the rest of string.
Here is another one-liner by using strpos (as suggested by #flu):
$needle = '233718_This_is_a_string';
$text = substr($needle, (strpos($needle, '_') ?: -1) + 1); // Returns This_is_a_string
I use strrchr(). For instance to find the extension of a file I use this function:
$string = 'filename.jpg';
$extension = strrchr( $string, '.'); //returns "jpg"
Another simple way, using strchr() or strstr():
$str = '233718_This_is_a_string';
echo ltrim(strstr($str, '_'), '_'); // This_is_a_string
In your case maybe ltrim() alone will suffice:
echo ltrim($str, '0..9_'); // This_is_a_string
But only if the right part of the string (after _) does not start with numbers, otherwise it will also be trimmed.
if anyone needs to extract the first part of the string then can try,
Query:
$s = "This_is_a_string_233718";
$text = $s."_".substr($s, 0, strrpos($s, "_"));
Output:
This_is_a_string
$string = "233718_This_is_a_string";
$withCharacter = strstr($string, '_'); // "_This_is_a_string"
echo substr($withCharacter, 1); // "This_is_a_string"
In a single statement it would be.
echo substr(strstr("233718_This_is_a_string", '_'), 1); // "This_is_a_string"
If you want to get everything after certain characters and if those characters are located at the beginning of the string, you can use an easier solution like this:
$value = substr( '123_String', strlen( '123_' ) );
echo $value; // String
Use this line to return the string after the symbol or return the original string if the character does not occur:
$newString = substr($string, (strrpos($string, '_') ?: -1) +1);

how to set a character betwen other characters in 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

Categories