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
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; 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.