In this post we will show you how to get substring between two strings in PHP.
we will show you different method for parse a string between two strings and How to extract content between two delimiters using PHP.
Method – 1
By usin get_between_data function, we can find substring between start and end.
in this function you have to pass start and end data and it will give you substring result.
function get_between_data($string, $start, $end)
{
$pos_string = stripos($string, $start);
$substr_data = substr($string, $pos_string);
$string_two = substr($substr_data, strlen($start));
$second_pos = stripos($string_two, $end);
$string_three = substr($string_two, 0, $second_pos);
// remove whitespaces from result
$result_unit = trim($string_three);
// return result_unit
return $result_unit;
}
$input_str = "test string start data end test string.";
$unit = get_between_data($input_str, 'start', 'end');
echo $unit; // Outputs: "data"
Method – 2
PHP - I Can't get the $_POST Values on Ajax Request
In this post we will give you information about PHP - I Can't get the $_POST Values on Ajax Request. Hear we will give you detail about PHP - I Can't get the $_POST Values on Ajax RequestAnd how to use it also give you demo for it if it is necessary.
I want to share this posts with you because when i was working on native PHP and Ajax Post request(Specially AngularJS Post request) at that time i can't get Post value on form submit. That's why i would like to share you this post. I also want to tell you i was working on my Ubuntu 14.04. I did try lot but i can't get value.
At last find stuff to how to solve this issue, i did use "file_get_contents('php://input')" that way i could get POST value in json object and i also decode json value using "json_decode()". So let's try this way :
Example:
$post = file_get_contents('php://input');
$post = json_decode($post);
$sql = "INSERT INTO items (title) VALUES ('".$post->title."')";
$result = $mysqli->query($sql);
Hope this code and post will helped you for implement PHP - I Can't get the $_POST Values on Ajax Request. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
By usin explode method we conver string in to array by “test” separator. so it will convert in to array and you abel to get thet data.
$input_str = "Xdata test HD01 test 1data";
$result_array = explode('test',$input_str);
print_r($result_array);
echo $result_array[1];
Method – 3
By usin this preg_replace and regex can get data between string. In this method we pass start and end data to get substring.
$input_str = "Xdata test HD01 test 1data";
$between = preg_replace('/(.*)test(.*)test(.*)/sm', '\2', $input_str);
echo $between;
Method – 4
By usin get_between_data function, we can find substring between start and end.
in this function you have to pass start and end data and it will give you substring result.
function get_between_data($input_str,$start,$end){
$result_array = explode($start, $input_str);
if (isset($result_array[1])){
$result_array = explode($end, $result_array[1]);
return $result_array[0];
}
// result null
return '';
}
$input_str = "Find data substring between two strings !!";
$start = "Find data ";
$end = " two strings !!";
$output_result = get_between_data($input_str,$start,$end);
echo $output_result;
Method – 5
By usin this preg_replace and regex can get data between string. In this method we pass start and end data to get substring.
$input_str = "[userid=256][userid=345]";
preg_match_all("/\[userid=([0-9]+)\]/", $input_str, $matches_res);
$modids_array = $matches_res[1];
foreach( $modids_array as $userid )
{
echo $userid."\n";
}