jcvenom Posted October 24, 2015 Posted October 24, 2015 (edited) I've been working on an emoji feature for a forum i'm building and I ran into a little problem. Firstly I was wondering would it be possible to create an array from a foreach loop like this? Then find all occurrences of string? And is what I've explained below possible Thank you $fetch_emojis = $ccm->query("SELECT * FROM `emojis`"); $num=0; while($e = $ccm->fetch_row($fetch_emojis)) { $num++; $emoji[] = $e['e_name']; } foreach ($emoji as $value) { $emojis = array( ':'.$value.':'=>''.$value.'' ); /*I would need something to check if a string starts with : and ends with :. Let's say the emoji code was :eyes: my code would find the string in the textarea then then remove the : colons from the string then it would find the string in the array i've created in the foreach loop and i'd continue from there */ echo $emoji[$_GET['t_c']]; } Edited October 24, 2015 by jcvenom Quote
Dayo Posted October 24, 2015 Posted October 24, 2015 why not just use str_replace i.e. <?php $emoji = array(":=)", ":=(", ":sick:"); $emojiImg = array("<img src='smile.png' />", "<img src='sad.png' />", "<img src='sick.png' />"); $string = ":=) hello im :sick: :=("; $output = str_replace($emoji, $emojiImg, $string); ?> Quote
jcvenom Posted October 24, 2015 Author Posted October 24, 2015 why not just use str_replace i.e. <?php $emoji = array(":=)", ":=(", ":sick:"); $emojiImg = array("<img src='smile.png' />", "<img src='sad.png' />", "<img src='sick.png' />"); $string = ":=) hello im :sick: :=("; $output = str_replace($emoji, $emojiImg, $string); ?> I have about 300 emojis, and I really don't want to type $emoji = array(':eyes:'=>'eyes'); //Now type that 300 times //instead i want to do this foreach ($emoji as $value) { $emojis = array( ':'.$value.':'=>''.$value.'' ); echo var_dump($emojis)."<br/>"; ); Quote
KyleMassacre Posted October 24, 2015 Posted October 24, 2015 You can create a simple function like: ParseEmojis($text) { global $ccm; $emoji = $ccm->query("select emoji_text, emoji_image from emojis"); while($r = $ccm->fetch_row($emoji)) { $eText[] = $r['emoji_text']; $eImage[] = $r['emoji_image']; } return str_ireplace($r['emoji_text'],$r['emoji_image'],$text); } Something like that. I am on my phone so it may not even work but you get the idea Quote
jcvenom Posted October 24, 2015 Author Posted October 24, 2015 You can create a simple function like: ParseEmojis($text) { global $ccm; $emoji = $ccm->query("select emoji_text, emoji_image from emojis"); while($r = $ccm->fetch_row($emoji)) { $eText[] = $r['emoji_text']; $eImage[] = $r['emoji_image']; } return str_ireplace($r['emoji_text'],$r['emoji_image'],$text); } Something like that. I am on my phone so it may not even work but you get the idea Close, but not entirely what I'm looking for, you see the text in the db for example would look like ( eyes ) and the emoji being inputted into the textarea on the userend would be :eyes:, therefore my code would search the content for :eyes:, then remove the : and the search the db for eyes and load its image Quote
KyleMassacre Posted October 24, 2015 Posted October 24, 2015 (edited) Something like: ParseEmojis($text) { global $ccm; $emoji = $ccm->query("select emoji_text, emoji_image from emojis"); while($r = $ccm->fetch_row($emoji)) { $eText[] = ":{$r['emoji_text']}:"; $eImage[] = $r['emoji_image']; } return str_ireplace($eText,$eImage,$text); } My original wouldnt have worked anyway Plus I am not really understanding the way you are doing it. It sounds like you are possibly adding more work than needed Edited October 24, 2015 by KyleMassacre Quote
IllegalPigeon Posted October 24, 2015 Posted October 24, 2015 I was going to reply with an answer, but it looks like it's been done. So instead I'll say this; You're asking for an answer to a very simple question, considering you've mastered PHP and you're so good at it. Quote
KyleMassacre Posted October 25, 2015 Posted October 25, 2015 But what I personally would have done is store the emoji string just the way the user would enter it. So instead of storing it as "eyes" I would have stored it as ":eyes:" then you wouldn't have to do any kind of fancy searching for it. To fix it you can run: update emojis set emoji_text = replace(emoji_text,emoji_text,concat(':',emoji_text,':')); Quote
jcvenom Posted October 25, 2015 Author Posted October 25, 2015 But what I personally would have done is store the emoji string just the way the user would enter it. So instead of storing it as "eyes" I would have stored it as ":eyes:" then you wouldn't have to do any kind of fancy searching for it. To fix it you can run: update emojis set emoji_text = replace(emoji_text,emoji_text,concat(':',emoji_text,':')); Thank you very much - - - Updated - - - I was going to reply with an answer, but it looks like it's been done. So instead I'll say this; You're asking for an answer to a very simple question, considering you've mastered PHP and you're so good at it. OK thanks anyway Quote
jcvenom Posted October 25, 2015 Author Posted October 25, 2015 (edited) But what I personally would have done is store the emoji string just the way the user would enter it. So instead of storing it as "eyes" I would have stored it as ":eyes:" then you wouldn't have to do any kind of fancy searching for it. To fix it you can run: update emojis set emoji_text = replace(emoji_text,emoji_text,concat(':',emoji_text,':')); I think this would've done the same thing if I used a regex //It wont let me post the regex code so its replacing it with an image in the forums so remove the " / " after the : $matches = regex($content, ':/(*){0:10}:'); foreach($m as $matches){ $ccm->query("SELECT `e_name` FROM `emojis` WHERE `e_name` = '".$ccm->escape($m)."'"); if($ccm->num_rows) $content = replace(':$m:', '<img ...>') } Edited October 25, 2015 by jcvenom Quote
KyleMassacre Posted October 25, 2015 Posted October 25, 2015 I think this would've done the same thing if I used a regex //It wont let me post the regex code so its replacing it with an image in the forums so remove the " / " after the : $matches = regex($content, ':/(*){0:10}:'); foreach($m as $matches){ $ccm->query("SELECT `e_name` FROM `emojis` WHERE `e_name` = '".$ccm->escape($m)."'"); if($ccm->num_rows) $content = replace(':$m:', '<img ...>') } Probably would but I believe using PCRE is a little bit more resource intensive than a str_replace(). Thats just what I have heard, I have never benchmarked it, and plus I am really bad at regex Quote
~Rob0t Posted October 26, 2015 Posted October 26, 2015 (edited) Why not use preg_replace_callback, as below; $string = preg_replace_callback("/(:\w+:)/", function($match) use ($emoji) { if( array_key_exists($match[0], $emoji) ) { return $emoji[$match[0]]; } }, $txt); https://eval.in/456767 You can also use emoji-font.css instead of images. ---- Also, you cannot be serious with this.... please don't be serious with that. regex($content, ':/(*){0:10}:'); Edited October 26, 2015 by ~Rob0t Quote
KyleMassacre Posted October 26, 2015 Posted October 26, 2015 Why not use preg_replace_callback, as below; $string = preg_replace_callback("/(:\w+:)/", function($match) use ($emoji) { if( array_key_exists($match[0], $emoji) ) { return $emoji[$match[0]]; } }, $txt); https://eval.in/456767 You can also use emoji-font.css instead of images. ---- Also, you cannot be serious with this.... please don't be serious with that. regex($content, ':/(*){0:10}:'); That would have been like my fall back suggestion. Quote
jcvenom Posted October 26, 2015 Author Posted October 26, 2015 Why not use preg_replace_callback, as below; $string = preg_replace_callback("/(:\w+:)/", function($match) use ($emoji) { if( array_key_exists($match[0], $emoji) ) { return $emoji[$match[0]]; } }, $txt); https://eval.in/456767 You can also use emoji-font.css instead of images. ---- Also, you cannot be serious with this.... please don't be serious with that. regex($content, ':/(*){0:10}:'); Thanks for this :) only question I have is how would I loop all the emojis from the database into an array? I really don't want to type array(':smile:'=>'<img src="so and so"/>'); 300 times Quote
Dayo Posted October 26, 2015 Posted October 26, 2015 This is very basic PHP, but if you want an example what DB connection are you using (PDO, mysqli_*, mysql_* etc ...) and whats the structure of the table you are looking from Quote
~Rob0t Posted October 26, 2015 Posted October 26, 2015 Thanks for this :) [...] You're welcome only question I have is how would I loop all the emojis from the database into an array? I really don't want to type array(':smile:'=>'<img src="so and so"/>'); 300 times This is a genuine question?! (tagging you in [MENTION=71663]IllegalPigeon[/MENTION]) Quote
jcvenom Posted October 26, 2015 Author Posted October 26, 2015 (edited) This is very basic PHP, but if you want an example what DB connection are you using (PDO, mysqli_*, mysql_* etc ...) and whats the structure of the table you are looking from I'm currently using mysqli. I think you're slightly mistaken I do realise this is basic PHP correct me if I was wrong, If I was using $emojis = array(':eyes:'=>'<img src="whatever"/>'); I could easily just use str_ireplace(array_keys($emojis),array_values($emojis), $string); But it's my fault for not giving you a clear understanding, basically I want to loop the emojis stored in my database and store them in an array but it's an unexpected challenge i'm facing any ideas would prove useful Edited October 26, 2015 by jcvenom Quote
IllegalPigeon Posted October 26, 2015 Posted October 26, 2015 Thanks for this :) only question I have is how would I loop all the emojis from the database into an array? I really don't want to type array(':smile:'=>'<img src="so and so"/>'); 300 times You seriously want to know how to store data in an array from the database? Like, seriously? When a few weeks ago you claimed to have "learnt OOP" in a few weeks? You know MySQLi is perfectly capable of fetching as associative array, right? Quote
jcvenom Posted October 26, 2015 Author Posted October 26, 2015 You seriously want to know how to store data in an array from the database? Like, seriously? When a few weeks ago you claimed to have "learnt OOP" in a few weeks? You know MySQLi is perfectly capable of fetching as associative array, right? Yes I know it's capable of that, and No I didn't claim to know anything I'm just asking for help and you seem to know more so I'd appreciate the help and not bringing things up thank you - - - Updated - - - You seriously want to know how to store data in an array from the database? Like, seriously? When a few weeks ago you claimed to have "learnt OOP" in a few weeks? You know MySQLi is perfectly capable of fetching as associative array, right? And I'm using procedural PHP, not OOP. After I'm done school I'm starting computer science and I'll learn it there Quote
IllegalPigeon Posted October 26, 2015 Posted October 26, 2015 Yes I know it's capable of that, and No I didn't claim to know anything I'm just asking for help and you seem to know more so I'd appreciate the help and not bringing things up thank you - - - Updated - - - And I'm using procedural PHP, not OOP. After I'm done school I'm starting computer science and I'll learn it there So...if you know it's capable of that, problem solved. Quote
jcvenom Posted October 26, 2015 Author Posted October 26, 2015 So...if you know it's capable of that, problem solved. I just need an example mate, I'm not arguing nor did I post this because I think I know it all, I'm just asking for help and if you seriously feel like not helping me maybe someone else will anyways thank you Quote
~Rob0t Posted October 26, 2015 Posted October 26, 2015 (edited) This is what you're looking for Fetches all result rows as an associative array, a numeric array, or both http://php.net/manual/en/mysqli-result.fetch-all.php Edited October 26, 2015 by ~Rob0t Quote
Sim Posted October 26, 2015 Posted October 26, 2015 I would either use array_replace() If I wanted some more customized code I would use array_walk() One must use store the emoji texts in an array first obviously =) Quote
jcvenom Posted October 26, 2015 Author Posted October 26, 2015 I would either use array_replace() If I wanted some more customized code I would use array_walk() One must use store the emoji texts in an array first obviously =) Very true, about the array :P Quote
KyleMassacre Posted October 26, 2015 Posted October 26, 2015 I would probably use anything with a callback in it. This way overtime as you develop your site more it maybe a lot easier to maintain some stuff. For example you can use output buffering and grab your buffer, run it through your callback whether it be preg_replace_callback() or array_walk() etc. After you perform that then you can display your modified buffer with everything done. This would work great for emojis or a bad word filter to name a couple things. Quote
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.