daneilair Posted January 17, 2010 Share Posted January 17, 2010 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 https://forums.phpfreaks.com/topic/188816-for-each-help/ Share on other sites More sharing options...
wildteen88 Posted January 17, 2010 Share Posted January 17, 2010 You'll want to use preg_match_all. Link to comment https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996835 Share on other sites More sharing options...
daneilair Posted January 18, 2010 Author Share Posted January 18, 2010 You'll want to use preg_match_all. 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 Link to comment https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996905 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 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 https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996908 Share on other sites More sharing options...
daneilair Posted January 18, 2010 Author Share Posted January 18, 2010 $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 https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996912 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 Its telling you that $matches[1] is an array and not a string. I would suggest print_r($matches) and look at your array structure. Link to comment https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996922 Share on other sites More sharing options...
daneilair Posted January 18, 2010 Author Share Posted January 18, 2010 This is what I got Array ( [0] => Array ( [0] => [video title="video1"][/video] [1] => [video title="video2"][/video] ) [1] => Array ( [0] => video1 [1] => video2 ) ) So it was an array but how would I break it down? Daniel Link to comment https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996926 Share on other sites More sharing options...
Andy-H Posted January 18, 2010 Share Posted January 18, 2010 $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 https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996929 Share on other sites More sharing options...
daneilair Posted January 18, 2010 Author Share Posted January 18, 2010 $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 https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996934 Share on other sites More sharing options...
daneilair Posted January 18, 2010 Author Share Posted January 18, 2010 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 https://forums.phpfreaks.com/topic/188816-for-each-help/#findComment-996951 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.