Jump to content

Tokenising and Iterating Through An Array


gaza165

Recommended Posts

$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 ([email protected];D ;D

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

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

}

Archived

This topic is now archived and is closed to further replies.

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