Get content between two delimiters in PHP – extract HTML comments using PHP

Quite a handy little function provided by BitRepository which enables you to grab the text between two delimiters in PHP.

Example:

[box type=”bio”]

Sentence: Hello *world, thanks for having a look at TurkeyTunnel^

Apply function: $extract = extract_unit($stringToExtractFrom,”* “,” ^”);

Result: world, thanks for having a look at TurkeyTunnel

[/box]

Now for the code:

[box type=”bio”]

function extract_unit($string, $start, $end)

{

$pos = stripos($string, $start);

$str = substr($string, $pos);

$str_two = substr($str, strlen($start));

$second_pos = stripos($str_two, $end);

$str_three = substr($str_two, 0, $second_pos);

$unit = trim($str_three); // remove whitespaces return $unit;

}

[/box]

Now a common question is: “How do I extract HTML comments from a CURL result set using PHP?”

Simple ?

[box type=”bio”]

$curl = curl_init($target_url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);

curl_close($curl);

//this is where the magic happens and all text between the comment tags is grabbed

$extract = extract_unit($result,”<!– “,” –>”);

[/box]

Cheerio ?

"Turkey