gaza165 Posted August 13, 2008 Share Posted August 13, 2008 $pattern = "/http:\/\/[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+/i"; $match = preg_match_all($pattern, $body, $matches,1); $gaz = $body; $i = 0; foreach($matches[0] as $url){ $replacement = "%TOKEN".$i."%"; $gaz = preg_replace("@$url@", $replacement, $gaz, 1); $i++; } $phrase = getSentences($gaz); foreach($matches as $value) { print_r($value); } //////////////////////////////////////////=------------------------------------------- Hello everyone, im stressed!! I have written a blog and am using preg_match and preg_replace to look for all urls that could contain an image that a user has posted, for example looking for http://www.domain.com/image.jpg... I am then tokenising these strings by replacing the matches with %TOKEN0% and incrementing it by 1 by each image. The problem i am having is converting the tokens back to their image urls and then displaying it. I know i need a foreach loop to loop through the matches and put the image urls in.... The reason i am doing this is so that users are able to put links to external images in but still being able to use htmlentities to stop from XSS and sql injection etc. Can someone please help me.... I am a very novice coder, so face palming will be a definate response. Cheers Garry Sheppard (gaza165@hotmail.com) ;D Quote Link to comment https://forums.phpfreaks.com/topic/119553-tokenising-and-iterating-through-an-array/ Share on other sites More sharing options...
ignace Posted August 13, 2008 Share Posted August 13, 2008 wouldn't it not just be simpler to create a function that writes out a <img /> element and validates that the image exists? Quote Link to comment https://forums.phpfreaks.com/topic/119553-tokenising-and-iterating-through-an-array/#findComment-615927 Share on other sites More sharing options...
gaza165 Posted August 13, 2008 Author Share Posted August 13, 2008 I have tried to encase the image url with the <img> tags but this still does not solve the problem of htmlentities, i would prefer to do it this way just need to know how to iterate through the array and replace the tokens 1 by 1.. Garry Quote Link to comment https://forums.phpfreaks.com/topic/119553-tokenising-and-iterating-through-an-array/#findComment-615931 Share on other sites More sharing options...
gaza165 Posted August 13, 2008 Author Share Posted August 13, 2008 Though i would put the entire code in for helpfullness!! PLEASE HELP!! SOMEONE MUST UNDERSTAND WHAT I AM DOING!!? Also there is a working example at http://test.thedesignmonkeys.co.uk/blog.php include ('dbconnect/dbconnect.php'); function getSentences($str, $num = 2, $separators = array(".","") ){ $pos = strlen($str); $count = 0; strip_tags($body, "<h1>,<h2>,<h3>"); for( $i=0; $i < strlen($str); $i++ ){ if(in_array($str{$i},$separators)){ $pos = $i; $count++; if($count == $num){ break; } } } return substr($str, 0, $pos); } $result = mysql_query("SELECT * FROM blog"); while($row = mysql_fetch_array($result)) { $blog_id = $row['id']; $sql = mysql_query("SELECT * FROM blog_comments where blog_id = '$blog_id'"); $numcom = mysql_num_rows($sql); $body = str_replace("’","'",$body); $body = str_replace('“','"',$body); $body = str_replace('”','"',$body); $body = strip_tags($body, "<h1>,<h2>,<h3>"); $body = $row['body']; $blog_date = $row['blog_created']; $blogdate = strtotime($blog_date); $blogdate = date(" l F j, Y, g:i a", $blogdate); echo "<div id='blogcomments'>"; echo "<h1>".htmlentities($row['title'])."</h1>"; echo "<p class='date'>". $blogdate. "</p>"; $pattern = "/http:\/\/[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+/i"; $match = preg_match_all($pattern, $body, $matches,1); $gaz = $body; $i = 0; foreach($matches[0] as $url){ $replacement = "%TOKEN".$i."%"; $gaz = preg_replace("@$url@", $replacement, $gaz, 1); $i++; } $phrase = getSentences($gaz); foreach($matches as $value) { print_r($value); } echo "<form action='' method='post'>"; echo "<p class='more'><a href='fullblog.php?id=".$row['id']."'>Read the rest of this post>>></a></p>"; echo "<p class='numcom'><a class='post' href='fullblog.php?id=".$row['id']."&#showcomments'>".$numcom." comment(s)</a></p>"; echo "</div>"; } $num = mysql_num_rows($result); if ($num == 0) { echo "<h1 class='norecords'>No posts in the database!</h1>"; } Quote Link to comment https://forums.phpfreaks.com/topic/119553-tokenising-and-iterating-through-an-array/#findComment-615959 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.