i have a string and im using preg_match_all to find some tags in that string:
$str = "
Line 1: This is a string
Line 2: [img]http://placehold.it/350x150[/img] Should not be [youtube]SDeWJqKx3Y0[/youtube] included.";
preg_match_all("~\[img](.+?)\[/img]~i", $str, $img);
preg_match_all("~\[youtube](.+?)\[/youtube]~i", $str, $youtube);
foreach ($img[1] as $key => $value) {
echo '<img src="'.$value.'" "/>';
}
foreach ($youtube[1] as $key => $value) {
echo '<iframe width="420" height="315" src="http://www.youtube.com/embed/'.$value.'" frameborder="0" allowfullscreen> </iframe>';
}
this will return exactly what is being echoes with the right values.
but what i actually want is to return the entire string with the the [img] and [youtube] tags replaced with the values from those foreach statements:
Line 1: This is a string
Line 2: <img src="http://placehold.it/350x150" "/> Should not be <iframe width="420" height="315" src="http://www.youtube.com/embed/SDeWJqKx3Y0" frameborder="0" allowfullscreen> </iframe> included.
i’m not looking for a 3rd party alternative, just plain php functions.
i’m thinking of using preg_match and some case and switch statements, but i didn’t succeeded yet
an ideas?
You can use preg_replace
With something like this.