Asked  7 Months ago    Answers:  5   Viewed   44 times

I need a function that returns the substring between two words (or two characters). I'm wondering whether there is a php function that achieves that. I do not want to think about regex (well, I could do one but really don't think it's the best way to go). Thinking of strpos and substr functions. Here's an example:

$string = "foo I wanna a cake foo";

We call the function: $substring = getInnerSubstring($string,"foo");
It returns: " I wanna a cake ".

Thanks in advance.

Update: Well, till now, I can just get a substring beteen two words in just one string, do you permit to let me go a bit farther and ask if I can extend the use of getInnerSubstring($str,$delim) to get any strings that are between delim value, example:

$string =" foo I like php foo, but foo I also like asp foo, foo I feel hero  foo";

I get an array like {"I like php", "I also like asp", "I feel hero"}.



If the strings are different (ie: [foo] & [/foo]), take a look at this post from Justin Cook. I copy his code below:

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);

$fullstring = 'this is my [tag]dog[/tag]';
$parsed = get_string_between($fullstring, '[tag]', '[/tag]');

echo $parsed; // (result = dog)
Tuesday, June 1, 2021
answered 7 Months ago

I have since found a relevant wikipedia article. It is not a NP complete problem, it can be done in O(mn) time using a dynamic programming algorithm.

In PHP I found the similar_text function very useful. Here's a code sample to retrieve a series of text emails and loop through them and find ones that are 90% similar to each other. Note: Something like this is NOT scalable:

// Gather all messages by a user into two identical associative arrays
$getMsgsRes = mysql_query(SELECT * FROM email_messages WHERE from = '$someUserID');
while($msgInfo = mysql_fetch_assoc($getMsgsRes))
    $msgsInfo1[] = $msgInfo;
    $msgsInfo2[] = $msgInfo;

// Loop over msgs and compare each one to every other
foreach ($msgsInfo1 as $msg1)
    foreach ($msgsInfo2 as $msg2)
        if ($similarity_pst > 90)
            echo "{$msg1['msgID']} is ${similarity_pst}% to {$msg2['msgID']}n";
Wednesday, March 31, 2021
answered 9 Months ago

You should use the tool that is designed for this type of job, parse_url



$urls = array('',

function clean_url($url) {
    $parts = parse_url($url);
    return $parts['scheme'] . '://' . $parts['host'] . $parts['path'];

foreach ($urls as $url) {
    echo clean_url($url) . "n";


·> php url.php                                                                                        
Wednesday, March 31, 2021
answered 9 Months ago

Hope this is working... Just try

echo (substr(strip_tags($mytext), 0, 25));

Wednesday, March 31, 2021
answered 9 Months ago

Just try with:

$date1  = '2013-11-15';
$date2  = '2014-02-15';
$output = [];
$time   = strtotime($date1);
$last   = date('m-Y', strtotime($date2));

do {
    $month = date('m-Y', $time);
    $total = date('t', $time);

    $output[] = [
        'month' => $month,
        'total' => $total,

    $time = strtotime('+1 month', $time);
} while ($month != $last);



array (size=4)
  0 => 
    array (size=2)
      'month' => string '11-2013' (length=7)
      'total' => string '30' (length=2)
  1 => 
    array (size=2)
      'month' => string '12-2013' (length=7)
      'total' => string '31' (length=2)
  2 => 
    array (size=2)
      'month' => string '01-2014' (length=7)
      'total' => string '31' (length=2)
  3 => 
    array (size=2)
      'month' => string '02-2014' (length=7)
      'total' => string '28' (length=2)
Saturday, May 29, 2021
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :