Skip to main content

Command Palette

Search for a command to run...

Regex capture group

Published
1 min readView as Markdown

Regex capture group example

// use App\Models\Tests\Question;

// // $question=Question::find(242317);

// $questions=Question::where('test_id',10413)->get();
// return $questions;

// $memberTestHistory=MemberTestHistory::find(79);
// return $memberTestHistory->memberTestDetails

function convertBase64IMG($imgSrc)
{
  $img = file_get_contents($imgSrc);

  // Encode the image string data into base64
  $data = base64_encode($img);

  // Display the output
  return $data;
}

$str =
  '<img with="100px" src="https://cms.hoc247.vn//images/courses/2021/20210405/HdTU0WHBzRwRwp2VqF7naHIWZiA5RQTHTlAmkojb.jpg" alt="no img" height="200px" />';

$pattern = '/(<img.*)src="(.*?)"(.*)/';
$str = preg_replace_callback(
  $pattern,
  function ($m) {
    dump($m);
    $base64 = convertBase64IMG($m[2]);
    return $m[1] . "src=\"data:image/png;base64, $base64\"" . $m[3];
  },
  $str
);

return $str;

More from this blog

Khang Nguyen

119 posts