PHP - strpos - search in all string without offset from X to Y - php

In strpos function we can define offset - where parser sholud start search our substring in string. I have to create something similar - I have to remove range offsets from string, for exacly:
$string = 'This is my string';
echo strpos($string, 'is my', 0);
and it will be return something position of substring is my in main $string.
But how to tell a PHP script to search in all string (like this script above) but not in position from X to Y? Is it possible?
Note:
I'm using mb_strpos() but I think, a solution will be very similar for both functions.
Thanks.

You need your custom function - that should work for you:
//$X<$Y
funciton getOffset($str, $needle, $X, $Y) {
$str1 = substr($str, 0, $X);
$str2 = substr($str, $Y);
$pos1 = strpos($str1, $needle, 0);
$pos2 = $Y + strpos($str1, 'is my', 0);
return false == $pos1 ? $pos2 : $pos1;
}

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

PHP: Get nth character from end of string

I'm sure there must be an easy way to get nth character from the end of string.
For example:
$NthChar = get_nth('Hello', 3); // will result in $NthChar='e'
Just do this
$rest = substr("abcdef", -3, 1); // returns 'd'
Like this:
function get_nth($string, $index) {
return substr($string, strlen($string) - $index - 1, 1);
}
from substr examples
// Accessing single characters in a string
// can also be achieved using "square brackets"
thus:
$str = "isogram";
echo $str[-1]; // m
echo $str[-2]; // a
echo $str[-3]; // r
<?php
function get_nth($string, $offset) {
$string = strrev($string); //reverse the string
return $string[$offset];
}
$string = 'Hello';
echo get_nth($string, 3);
As $string[3] will give you the 3rd offset of the string, but you want it backwards, you need to string reverse it.
Edit:
Despite other answers (posted after mine) are using substring, and it can be a one liner, it is hardly readable to have substr($string, -2, 1), then just reverse the string and output the offset.
substr($string, -3);//returns 3rd char from the end of the string

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

defining the negative start parameter in substr as a variable

Is using the following correct for defining the negative start parameter for a substr,
because its the only way i know how to get it to retur the correct result.
$start == (-44);
or
$start == (int) -44;
$pair = substr($str, $start, 4);
the substr call is valid, the only error in your code (posted here) is the == operator.
It should be:
$start = -44;
$pair = substr($str, $start, 4)
Also is the start value -44 the 44th character from start or the end. The above code considers -44 to mean 44th character from end of string.
One more error you could run into is if the length of $str is less than 44.
You can just add a - before an expression (including a variable) to invert its sign:
$pair = substr($str, -$start, 4);
Or
$pair = substr($str, -44, 4);

string between, php

as I am new to php, and after googling :) I still could not find what I wanted to do.
I have been able to find start and end position in string that i want to extract but most of the example use strings or characters or integers to get string between but I could not find string bewteen two positions.
For example:
$string = "This is a test trying to extract";
$pos1 = 9; $pos2 = 14;
Then I get lost. I need to get the text between position 9 and 14 of of the string.
Thanks.
$startIndex = min($pos1, $pos2);
$length = abs($pos1 - $pos2);
$between = substr($string, $startIndex, $length);
You can use substr() to extract part of a string. This works by setting the starting point and the length of what you want to extract.
So in your case this would be:
$string = substr($string,9,5); /* 5 comes from 14-9 */
<?php
$string = "This is a test trying to extract";
$pos1 = 9;
$pos2 = 14;
$start = min($pos1, $pos2);
$length = abs($pos1 - $pos2);
echo substr($string, $start - 1, $length); // output 'a test'
?>

Categories