Sam Granger Posted February 4, 2008 Share Posted February 4, 2008 Hey! I have a pdf class, and I can use it to output images. I want to get info from the database. The content could look like this for example: text text text, more text [image]hello.jpg[/image] haha hello [thumb]hi.gif[/thumb]. haha more text Anyway, you get the point . How do I get the contents out of the tag and put them into images? I think array? or? I need to use a seperate code for text and images to generate the pdf. For text: $pdf->ezText("$title", 22); And for images: $pdf->ezImage('http://www.domain.com/logo.jpg', '0', '150', 'none', 'left'); for each image. Help! Do I use preg_match? Quote Link to comment https://forums.phpfreaks.com/topic/89441-solved-getting-contents-between-tags-in-string-and-output-them-one-by-one/ Share on other sites More sharing options...
Sam Granger Posted February 4, 2008 Author Share Posted February 4, 2008 Just to make it clear, I don't need to input the images inbetween the text, I will strip the text for the tags+filenames, output that and then will output all images at end. Quote Link to comment https://forums.phpfreaks.com/topic/89441-solved-getting-contents-between-tags-in-string-and-output-them-one-by-one/#findComment-458017 Share on other sites More sharing options...
Sam Granger Posted February 5, 2008 Author Share Posted February 5, 2008 Hope someone can answer this Quote Link to comment https://forums.phpfreaks.com/topic/89441-solved-getting-contents-between-tags-in-string-and-output-them-one-by-one/#findComment-458657 Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 This should do what you need. It will strip out all the tags and populate them into an array. It won't handle nested tags though. I also added in some basic whitespace handling to help make the returned text look better. <?php function matchCallback ( $matches ) { global $tags; list(,$tag,$contents,$suffix) = $matches; //Add Tag if(!isset($tags[$tag])) $tags[$tag] = array(); $tags[$tag][] = $contents; //Make replacement $rVal = ''; return (preg_match('/[a-zA-Z0-9]/',trim($suffix))) ? ' '.trim($suffix) //Found a charachter, add a space : trim($suffix); //Found something else, no space } $text = "text text text, more text [image]hello.jpg[/image] haha hello [thumb]hi.gif[/thumb]. haha more text"; $tags = array(); $text = preg_replace_callback('/\s*\[(\w+)\](.+?)\[\/\1\](\s*.?)/',matchCallback,$text); print_r($tags); print $text; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89441-solved-getting-contents-between-tags-in-string-and-output-them-one-by-one/#findComment-458678 Share on other sites More sharing options...
Sam Granger Posted February 5, 2008 Author Share Posted February 5, 2008 Awesome that works! How would I go about echoing the tag contents one by one in a loop/while statement? Quote Link to comment https://forums.phpfreaks.com/topic/89441-solved-getting-contents-between-tags-in-string-and-output-them-one-by-one/#findComment-458724 Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 <?php ... $text = "text text text, more text [image]hello.jpg[/image] haha hello [thumb]hi.gif[/thumb]. haha more text"; $tags = array(); $text = preg_replace_callback('/\s*\[(\w+)\](.+?)\[\/\1\](\s*.?)/',matchCallback,$text); //Go through all of them foreach($tags as $tagname=>$items){ print "<b>{$tagname}</b><ul>"; foreach($items as $item) print "<li>{$item}</li>"; } //Just image tags if(is_array($tags['image'])){ foreach($tags['image'] as $item) $pdf->ezImage($item, '0', '150', 'none', 'left'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89441-solved-getting-contents-between-tags-in-string-and-output-them-one-by-one/#findComment-458731 Share on other sites More sharing options...
Sam Granger Posted February 5, 2008 Author Share Posted February 5, 2008 Thanks! Topic solved!! Quote Link to comment https://forums.phpfreaks.com/topic/89441-solved-getting-contents-between-tags-in-string-and-output-them-one-by-one/#findComment-458736 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.