Jump to content

embedded image tag in string


jason9

Recommended Posts

i want to be able to extract a string from a variable when an IMG tag is present and then use the link of the image to create an image tag of it's own to be displayed

 

let's say i have a string $post which contains the following... (ignoring the quotations)

 

here is some random text ["IMG"]http://www.somedomain.com/someimage.jpg[/"IMG"] here is some more random text

 

how would i be able to extract the string inside the tags and replace it with a different string in the same position excluding the tags such as

 

<img src=''http://www.somedomain.com/someimage.jpg"/>

 

any recommendations on how i would be able to do this?

Link to comment
https://forums.phpfreaks.com/topic/253213-embedded-image-tag-in-string/
Share on other sites

i ended up using this cause i could not get the bbcode_parser to work

 

function bb_parse($string) { 
        $tags = 'url|URL|img|IMG|video|VIDEO'; 
        while (preg_match_all('`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) foreach ($matches[0] as $key => $match) { 
            list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]); 
            switch ($tag) {
                case 'url':
	case 'URL':
		$replacement = '<a href="' . ($param? $param : $innertext) . "\">$innertext</a>";
		break; 
                case 'img':
	case 'IMG':
                    list($width, $height) = preg_split('`[Xx]`', $param); 
                    $replacement = "<img width='450' src=\"$innertext\" " . (is_numeric($width)? "width=\"$width\" " : '') . (is_numeric($height)? "height=\"$height\" " : '') . '/><br/>'; 
                break; 
                case 'video': 
	case 'VIDEO': 
                    $videourl = parse_url($innertext); 
                    parse_str($videourl['query'], $videoquery); 
                    if (strpos($videourl['host'], 'youtube.com') !== FALSE) $replacement = "<iframe width='450' height='300' src='http://www.youtube.com/embed/" . $videoquery['v'] . "' frameborder='0' allowfullscreen></iframe><br/>"; 
                break; 
            } 
            $string = str_replace($match, $replacement, $string); 
        } 
        return $string; 
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.