IT & Programming

Find a string between 2 points with PHP

  • How to find string between 2 strings with PHP.
  • Find a string between two tags with PHP.
  • Search a text between 2 points.

This function accepts 3 parameters and returns the string between 2 points.

Function

function findString($startPoint, $endPoint, $source) {

    $m=array();
    preg_match_all('/' . preg_quote($startPoint, '/') . '([^)]+)'. preg_quote($endPoint, '/').'/i', $source, $m);
    return $m[1][0];
}

Calling The Function

$startPoint='< title>';
$endPoint='< /title>';
$source='< title>welcome to my website< /title>'

echo findString($startPoint, $endPoint, $source);

Output

welcome to my website

Comments

  1. Herman Strauss May 05, 2013

    Thanks a lot!!

  2. Sazi February 02, 2013

    So much thanks , i was looking for this pattern

  3. Ezequiel Mendez September 09, 2011

    Awesome thanks a lot!

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *