PHP Cut String Function

Published on June 30, 2025
<?php
/*
* cut function
*
* @param string $data
* @param string $start
* @param string $end
* @return string
*
* Example:
* $data = "Hello, this is a test. I want to cut this part.";
* echo cut($data, "this is ", " part"); // Output: "a test."
* This function extracts a substring from a given string based on specified start and end tokens.
* It returns the text found between the first occurrence of the start token and the first occurrence
* of the end token after the start token.
*/

function cutWithExplode($data, $start, $end){
$data = explode($start, $data);
$data = explode($end, $data[1]);
return $data[0];
}

function cut($data, $start, $end){
$start = strpos($data, $start)+strlen($start);
$end = strpos($data, $end, $start);
return substr($data, $start, $end - $start);
}


© 2025 My Awesome Site. All rights reserved.