3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $string = "ayy lmao tag: anal, dick pornstar: James Deen channel:x-Art" . " end:"; $pattern = "/.+?(?=(tag|pornstar|channel|end)+:)/"; preg_match_all($pattern, $string, $matches); $meme = array(); $final = array(); for($i = 0; $i < sizeof($matches[0]); $i++) { $meme[$i] = explode(":", $matches[0][$i]); } $sql = "SELECT "; for($i = 0; $i < sizeof($meme); $i++) { if(sizeof($meme[$i]) == 1) { $sql .= "`videos`.`name`"; } else { $sql .= "`" . $meme[$i][0] . "s`.`name`"; } if($i != sizeof($meme)-1) { $sql .= ", "; } } $sql .= "\nFROM `videos`\n"; for($i = 0; $i < sizeof($meme); $i++) { if(sizeof($meme[$i]) == 2) { $sql .= "LEFT JOIN `video_" . $meme[$i][0] . "s` ON `video_" . $meme[$i][0] . "s`.`video` = `videos`.`id`\n"; $sql .= "LEFT JOIN `" . $meme[$i][0] . "s` ON `" . $meme[$i][0] . "s`.`id` = `video_`" . $meme[$i][0] . "s`.`" . $meme[$i][0] . "`\n"; } } $sql .= "\nWHERE\n"; for($i = 0; $i < sizeof($meme); $i++) { if(sizeof($meme[$i]) == 1) { $sql .= "`videos`.`name` LIKE '% " . $meme[$i][1] . "%'\n"; } else { $sql .= "`" . $meme[$i][0] . "`.`name` LIKE '%" . $meme[$i][1] . "%'\n"; } if($i != sizeof($meme)-1) { $sql .= "\nAND\n"; } } print_r($matches); print_r($meme); print_r($sql); ?>
based on 0dld3
Output for 5.6.0 - 7.3.0beta1
Notice: Undefined offset: 1 in /in/RBCaF on line 41 Array ( [0] => Array ( [0] => ayy lmao [1] => tag: anal, dick [2] => pornstar: James Deen [3] => channel:x-Art ) [1] => Array ( [0] => tag [1] => pornstar [2] => channel [3] => end ) ) Array ( [0] => Array ( [0] => ayy lmao ) [1] => Array ( [0] => tag [1] => anal, dick ) [2] => Array ( [0] => pornstar [1] => James Deen ) [3] => Array ( [0] => channel [1] => x-Art ) ) SELECT `videos`.`name`, `tags`.`name`, `pornstars`.`name`, `channels`.`name` FROM `videos` LEFT JOIN `video_tags` ON `video_tags`.`video` = `videos`.`id` LEFT JOIN `tags` ON `tags`.`id` = `video_`tags`.`tag` LEFT JOIN `video_pornstars` ON `video_pornstars`.`video` = `videos`.`id` LEFT JOIN `pornstars` ON `pornstars`.`id` = `video_`pornstars`.`pornstar` LEFT JOIN `video_channels` ON `video_channels`.`video` = `videos`.`id` LEFT JOIN `channels` ON `channels`.`id` = `video_`channels`.`channel` WHERE `videos`.`name` LIKE '% %' AND `tag`.`name` LIKE '% anal, dick %' AND `pornstar`.`name` LIKE '% James Deen %' AND `channel`.`name` LIKE '%x-Art %'