jason9 Posted December 15, 2011 Share Posted December 15, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253213-embedded-image-tag-in-string/ Share on other sites More sharing options...
kicken Posted December 15, 2011 Share Posted December 15, 2011 Google for a bbcode parser library. It wll parse out sequences like that. Save yourself the trouble of having to try and code it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/253213-embedded-image-tag-in-string/#findComment-1298064 Share on other sites More sharing options...
jason9 Posted December 15, 2011 Author Share Posted December 15, 2011 thanks i'll check that out Quote Link to comment https://forums.phpfreaks.com/topic/253213-embedded-image-tag-in-string/#findComment-1298065 Share on other sites More sharing options...
jason9 Posted December 15, 2011 Author Share Posted December 15, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/253213-embedded-image-tag-in-string/#findComment-1298099 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.