gaza165 Posted October 5, 2008 Share Posted October 5, 2008 I am retrieving my blog from the database, at the moment i am search for images, youtube videos and a links. As you can see from the code I am using preg_match and preg_replace to search for their particular patterns. I am then using str_replace to replace the values on output with their appropriate values. So say the image links are posted into the database like.... http://www.domain.com/image.jpg i then replace that with %TOKEN0% once this is all done i echo out echo $string; Basically what i want to do is be able to echo out the image contained in the $string as a seperate item.... soo i can do... echo $string and then echo $image Hope someone can help.... thanks... <?php // Pattern and pregmatches etc \\ $pattern = "%http://\S+\.(?:jpe?g|png|gif)%i"; $match = preg_match_all($pattern, $body, $matches,1); $gaz = htmlentities($body); $a = 0; foreach($matches[0] as $url){ $replacement = "%TOKEN".$a."%"; $gaz = preg_replace("@$url@", $replacement, $gaz, 1); $a++; } $vidpattern = "%http://(?:\S+\.swf\b|\S+?youtube\S+)%"; $vidmatch = preg_match_all($vidpattern, $gaz, $vidmatches); $g = 0; foreach($vidmatches[0] as $vidurl) { $replace = '%YOUTUBE'.$g.'%'; $gaz = preg_replace($vidpattern,$replace, $gaz, 1); $g++; } $linkpattern = "/http:\/\/[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+/i"; $linkmatch = preg_match_all($linkpattern, $gaz, $linkmatches); $b = 0; foreach($linkmatches[0] as $linkurl) { $replacement = "%LINK".$b."%"; $gaz = preg_replace("@$linkurl@", $replacement, $gaz, 1); $b++; } // End of replace with Tokens!! \\ $string = getSentences($gaz); $c = 0; foreach($matches[0] as $value) { $replace = "<a href='".$value."'><img class='homeimg' src='".$value."'></a>"; $search = "%TOKEN".$c."%"; $string = str_replace($search, $replace, $string); $c++; } $d= 0; foreach($linkmatches[0] as $linkurl) { $search = "%LINK".$d."%"; $replace = "<a href='".$linkurl."' class='bloglink' encode>$linkurl</a>"; $string = str_replace($search, $replace, $string); $d++; } $e= 0; foreach($vidmatches[0] as $vidurl) { $vidurl = substr($vidurl, 31); $search = "%YOUTUBE".$e."%"; $replace = '<embed src="http://www.youtube.com/v/'.$vidurl.'" type="application/x-shockwave-flash" wmode="transparent" class="youtube" width="425" height="350" />'; $string = str_replace($search, $replace, $string); $e++; } echo "<p class='mainblog'>".nl2br($string)."</p>"; //output the blog... echo "<div id='maincomments'>".$numcom."</div>"; $blog_date = $row['blog_created']; $blogdate = strtotime($blog_date); $blogdate = date("g:i a", $blogdate); $tagarray = explode(", ", $row['tags']); echo "</div>"; } $num = mysql_num_rows($result); if ($num == 0) { echo "<h1 class='norecords'>No posts in the database!</h1>"; } ?> Link to comment https://forums.phpfreaks.com/topic/127102-tokenising-my-blog-need-image-to-seperate-variable/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.