Home > front end >  Transforming tag with regex replace
Transforming tag with regex replace

Time:02-01

I have an tag to convert. I'm try do this with php functions: How i can do that transformation?

content before @[youtube](XIMLoLxmTDw) content after =>  content before <div ><iframe src="https://www.youtube.com/embed/XIMLoLxmTDw" frameborder="0" allowfullscreen="true"></iframe></div> content after

CodePudding user response:

function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from) strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}
$string='<div ><iframe src="https://www.youtube.com/embed/XIMLoLxmTDw" frameborder="0" allowfullscreen="true"></iframe></div>';
$start='embed/';
 $end='"';
$idYoutube=getStringBetween($string , $start,$end);
echo $idYoutube;

CodePudding user response:

I'm not sure if I understand you but perhaps this code snippet can help you?

<?php

$re = '/\@\[youtube\]\((.*)\)/';
$str = '@[youtube](XIMLoLxmTDw)';
$subst = '<div ><iframe src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen="true"></iframe></div>';

$result = preg_replace($re, $subst, $str, 1);

echo $result;

Proof of concept on Regex101

  •  Tags:  
  • Related