Jump to content

For Each Help?


daneilair

Recommended Posts

Okay I'm making a blog-type-thing and I have a script to replace

[video title="thing"][/video]

with and embed code and it works great but the problem is when there are multiple occurrences of it then it doesn't work. Heres the script I have to do the replacing:

 

$article = $row['post'];
$matches = array();
preg_match('/\[video title="([^"]*)"\]\[\/video\]/', $article, $matches);
if(!empty($matches)) {
$vtitle = $matches[1];
$vtitleclean = str_replace(" ", "_", $vtitle);

$vfultitle = $vtitleclean . md5($vtitleclean) . ".flv";

$old = "[video title=\"" . $vtitle . "\"][/video]";
$new = "<object classid=\"clsid:F08DF954-8592-11D1-B16A-00C0F0283628\" id=\"PlayerObj\" width=\"400\" height=\"468\">
		<param name=\"quality\" value=\"high\">
		<param name=\"bgcolor\" value=\"#000000\">
		<param name=\"flashvars\" value=\"sv=" . $vfultitle . "&skin=incs/video player/SkinOverPlaySeekMute.swf&width=468&height=400\">
		<param name=\"width\" value=\"468\">
		<param name=\"height\" value=\"400\">
		<param name=\"scale\" value=\"noscale\">
		<param name=\"salign\" value=\"tl\">
		<param name=\"name\" value=\"Player\">
		<param name=\"type\" value=\"application/x-shockwave-flash\">
		<param name=\"pluginspage\" value=\"http://www.adobe.com/go/getflashplayer\">
		<embed src=\"incs/video/player.swf\" quality=\"high\" bgcolor=\"#000000\" flashvars=\"sv=videos/" . $vfultitle . "&skin=incs/video/SkinOverPlaySeekMute.swf&width=468&height=400\" width=\"468\" height=\"400\" scale=\"noscale\" salign=\"tl\" name=\"Player\" type=\"application/x-shockwave-flash\"pluginspage=\"http://www.adobe.com/go/getflashplayer\"> </embed>
	</object>";
$blog_post = str_replace($old, $new, $blog_post);
}

 

but when there there are more than one things to replace only the first one works. I'm assuming I'd use the "foreach() function but I don't know what to do. If anyone could help me out then that would be great.

 

Thanks,

Daniel

Link to comment
Share on other sites

When I try this it doesn't show anything even thought it should show 2 movies... Instead I get the error:

Warning: md5() expects parameter 1 to be string, array given in D:\www\projects\Websites\site\pages\blog.php on line 66

 

Daniel

 

Where is the md5() function called? Post the relevant code.

Link to comment
Share on other sites

			$article = $row['post']; /*Would be [video title="video1"][/video] [video title="video2"][/video]*/
		$matches = array();
		preg_match_all('/\[video title="([^"]*)"\]\[\/video\]/', $article, $matches);
		if(!empty($matches)) {
			$vtitle = $matches[1];
			$vtitleclean = str_replace(" ", "_", $vtitle);

			$vfultitle = $vtitleclean . md5($vtitleclean) . ".flv"; //<--------HERE

			$old = "[video title=\"" . $vtitle . "\"][/video]";
			$new = "<object classid=\"clsid:F08DF954-8592-11D1-B16A-00C0F0283628\" id=\"PlayerObj\" width=\"400\" height=\"468\">
					<param name=\"quality\" value=\"high\">
					<param name=\"bgcolor\" value=\"#000000\">
					<param name=\"flashvars\" value=\"sv=" . $vfultitle . "&skin=incs/video player/SkinOverPlaySeekMute.swf&width=468&height=400\">
					<param name=\"width\" value=\"468\">
					<param name=\"height\" value=\"400\">
					<param name=\"scale\" value=\"noscale\">
					<param name=\"salign\" value=\"tl\">
					<param name=\"name\" value=\"Player\">
					<param name=\"type\" value=\"application/x-shockwave-flash\">
					<param name=\"pluginspage\" value=\"http://www.adobe.com/go/getflashplayer\">
					<embed src=\"incs/video/player.swf\" quality=\"high\" bgcolor=\"#000000\" flashvars=\"sv=videos/" . $vfultitle . "&skin=incs/video/SkinOverPlaySeekMute.swf&width=468&height=400\" width=\"468\" height=\"400\" scale=\"noscale\" salign=\"tl\" name=\"Player\" type=\"application/x-shockwave-flash\"pluginspage=\"http://www.adobe.com/go/getflashplayer\"> </embed>
				</object>";
			$blog_post = str_replace($old, $new, $blog_post);
		}

Link to comment
Share on other sites

$article = $row['post'];
$matches = array();
preg_match_all('/\[video title="([^"]*)"\]\[\/video\]/', $article, $matches);

if(!empty($matches)) {

foreach($matches[1] as $vtitle) {

	$vtitleclean = str_replace(" ", "_", $vtitle);

	$vfultitle = $vtitleclean . md5($vtitleclean) . ".flv";

	$old = "[video title=\"" . $vtitle . "\"][/video]";
	$new = "<object classid=\"clsid:F08DF954-8592-11D1-B16A-00C0F0283628\" id=\"PlayerObj\" width=\"400\" height=\"468\">
			<param name=\"quality\" value=\"high\">
			<param name=\"bgcolor\" value=\"#000000\">
			<param name=\"flashvars\" value=\"sv=" . $vfultitle . "&skin=incs/video player/SkinOverPlaySeekMute.swf&width=468&height=400\">
			<param name=\"width\" value=\"468\">
			<param name=\"height\" value=\"400\">
			<param name=\"scale\" value=\"noscale\">
			<param name=\"salign\" value=\"tl\">
			<param name=\"name\" value=\"Player\">
			<param name=\"type\" value=\"application/x-shockwave-flash\">
			<param name=\"pluginspage\" value=\"http://www.adobe.com/go/getflashplayer\">
			<embed src=\"incs/video/player.swf\" quality=\"high\" bgcolor=\"#000000\" flashvars=\"sv=videos/" . $vfultitle . "&skin=incs/video/SkinOverPlaySeekMute.swf&width=468&height=400\" width=\"468\" height=\"400\" scale=\"noscale\" salign=\"tl\" name=\"Player\" type=\"application/x-shockwave-flash\"pluginspage=\"http://www.adobe.com/go/getflashplayer\"> </embed>
		</object>";
	$blog_post = str_replace($old, $new, $blog_post);
}
}

 

 

That should work, assuming $blog_post is set somewhere above the code posted?

 

//EDIT : Formatting F***ed up

Link to comment
Share on other sites

$article = $row['post'];
$matches = array();
preg_match_all('/\[video title="([^"]*)"\]\[\/video\]/', $article, $matches);

if(!empty($matches)) {

foreach($matches[1] as $vtitle) {

	$vtitleclean = str_replace(" ", "_", $vtitle);

	$vfultitle = $vtitleclean . md5($vtitleclean) . ".flv";

	$old = "[video title=\"" . $vtitle . "\"][/video]";
	$new = "<object classid=\"clsid:F08DF954-8592-11D1-B16A-00C0F0283628\" id=\"PlayerObj\" width=\"400\" height=\"468\">
			<param name=\"quality\" value=\"high\">
			<param name=\"bgcolor\" value=\"#000000\">
			<param name=\"flashvars\" value=\"sv=" . $vfultitle . "&skin=incs/video player/SkinOverPlaySeekMute.swf&width=468&height=400\">
			<param name=\"width\" value=\"468\">
			<param name=\"height\" value=\"400\">
			<param name=\"scale\" value=\"noscale\">
			<param name=\"salign\" value=\"tl\">
			<param name=\"name\" value=\"Player\">
			<param name=\"type\" value=\"application/x-shockwave-flash\">
			<param name=\"pluginspage\" value=\"http://www.adobe.com/go/getflashplayer\">
			<embed src=\"incs/video/player.swf\" quality=\"high\" bgcolor=\"#000000\" flashvars=\"sv=videos/" . $vfultitle . "&skin=incs/video/SkinOverPlaySeekMute.swf&width=468&height=400\" width=\"468\" height=\"400\" scale=\"noscale\" salign=\"tl\" name=\"Player\" type=\"application/x-shockwave-flash\"pluginspage=\"http://www.adobe.com/go/getflashplayer\"> </embed>
		</object>";
	$blog_post = str_replace($old, $new, $blog_post);
}
}

 

 

That should work, assuming $blog_post is set somewhere above the code posted?

 

//EDIT : Formatting F***ed up

 

Warning: Invalid argument supplied for foreach()

So I'm assuming that you can't use $matches[1] but what else is there?

 

Link to comment
Share on other sites

Okay I finally got it! Heres he code I used.

			$article = $row['post'];
		$matches = array();
		preg_match_all('/\[video title="([^"]*)"\]\[\/video\]/', $article, $matches);

		if(!empty($matches)) {
			for($i = 0; $i < count($matches[1]); $i++) {
				$vtitle = $matches[1][$i];
				$vtitleclean = str_replace(" ", "_", $vtitle);
				$vfultitle = $vtitleclean . md5($vtitleclean) . ".flv";

				$old = "[video title=\"" . $vtitle . "\"][/video]";
				$new = "<object classid=\"clsid:F08DF954-8592-11D1-B16A-00C0F0283628\" id=\"PlayerObj\" width=\"400\" height=\"468\">
					<param name=\"quality\" value=\"high\">
					<param name=\"bgcolor\" value=\"#000000\">
					<param name=\"flashvars\" value=\"sv=" . $vfultitle . "&skin=incs/video player/SkinOverPlaySeekMute.swf&width=468&height=400\">
					<param name=\"width\" value=\"468\">
					<param name=\"height\" value=\"400\">
					<param name=\"scale\" value=\"noscale\">
					<param name=\"salign\" value=\"tl\">
					<param name=\"name\" value=\"Player\">
					<param name=\"type\" value=\"application/x-shockwave-flash\">
					<param name=\"pluginspage\" value=\"http://www.adobe.com/go/getflashplayer\">
					<embed src=\"incs/video/player.swf\" quality=\"high\" bgcolor=\"#000000\" flashvars=\"sv=videos/" . $vfultitle . "&skin=incs/video/SkinOverPlaySeekMute.swf&width=468&height=400\" width=\"468\" height=\"400\" scale=\"noscale\" salign=\"tl\" name=\"Player\" type=\"application/x-shockwave-flash\"pluginspage=\"http://www.adobe.com/go/getflashplayer\"> </embed>
				</object>";
				$blog_post = str_replace($old, $new, $blog_post);
			}
		}

 

Thanks for the help guys!

 

Daniel

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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