Kingy Posted January 20, 2008 Share Posted January 20, 2008 within my script i have 3 varibles $ex[3] $ex[4] $ex[5] when someone writes a sentence it will appear [:hi] [my] [name is kingy] NB: BRACKETS REPRESENT THE ABOVE VARIBLES (the brackets dont exist) what my script with then do is count the characters in the sentence and store them. but what i have done is, becuase the user doesn't actually type the : i've replaced it so that its not there.. but now, what i want to do is count the amount of times a smiley or sad face has been written, but because i have replaced the colon from $ex[3] it won't count the smiley face because there won't be any colon. My question is how can i get rid of the first : without getting rid of the rest? Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 20, 2008 Share Posted January 20, 2008 if the colon is always the first character in the first variable you could write some code like this str_replace (':', '', $ex[3], 1); this will limit the replace to just one then you can count the smile faces Scott. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 20, 2008 Share Posted January 20, 2008 I'm a little confused. If the user does not type the first colon, where does it come from? Perhaps showing us some of your code (using tags) will help Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 20, 2008 Share Posted January 20, 2008 if the colon is always the first character in the first variable you could write some code like this str_replace (':', '', $ex[3], 1); this will limit the replace to just one then you can count the smile faces Scott. Im afraid not. The 4th (and optional) parameter of the str_replace() function is the name of variable that you wish to store the number of replacements made. Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 20, 2008 Author Share Posted January 20, 2008 GingerRobot this script is a php irc bot I'll post what the bot reads when something is written on the irc :Kingy!k1hosting@I.am.the.King PRIVMSG #Lobby :hello :Kingy!k1hosting@I.am.the.King PRIVMSG #Lobby :test as you can see it has the : before anything that is typed Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 20, 2008 Share Posted January 20, 2008 Ok, i see. The substr() function is your friend: <?php $str = ':Hello'; $str = substr($str,1); echo $str;//Hello ?> Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 20, 2008 Author Share Posted January 20, 2008 ok thanks for that, now to get the smileys, i would use something like preg_match_all() ?? what would be the basic code for this i have combined $ex[3] $ex[4] and $ex[5] into one. -> $string preg_match_all("", $string); i think thats way wrong? but something similar? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 20, 2008 Share Posted January 20, 2008 Almost. You need preg_replace. You can also use an array of different patterns and replacements: <?php $patterns[0] = '|:\)|'; $patterns[1] = '|:\(|'; $patterns[2] = '||i';//case insensitive $replacements[0] = '<img src="path_to_happy_face />"'; $replacements[1] = '<img src="path_to_sad_face />"'; $replacements[2] = '<img src="path_to_very_happy_face />"'; $string = preg_replace($patterns,$replacements,$string); ?> If you add to that, make sure the key of the pattern matches with the key of the replacement. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 20, 2008 Share Posted January 20, 2008 Having said that, if you're just going to be using smilies, and no bb tags, str_ireplace would be more appropriate: <?php $patterns[] = ''; $patterns[] = ''; $patterns[] = ''; $replacements[] = '<img src="path_to_happy_face />"'; $replacements[] = '<img src="path_to_sad_face />"'; $replacements[] = '<img src="path_to_very_happy_face />"'; $string = str_ireplace($patterns,$replacements,$string); ?> Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 20, 2008 Author Share Posted January 20, 2008 ok thats not really what im looking for... i'll explain this properly eg: $string = some writing blah blah more writing what i want to do is search that $string and match any smiley faces that are found FOUND 1 SMILE and then if any are found ill do a sql query $smile = $row['smile']; then do an sql update adding however many smilies are found the only problem i have is finding out how many there are Quote Link to comment Share on other sites More sharing options...
resago Posted January 21, 2008 Share Posted January 21, 2008 use replace, but save it to a different variable and replace the smiles with any single character. then count the length of the string. Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 21, 2008 Author Share Posted January 21, 2008 Well at the moment i have... <?php $string = "hi there "; if (preg_match("", "$string")) { echo "A Match"; } else { echo "No Match"; } ?> which returns the error: PHP Warning: preg_match(): No ending delimiter ':' found in test.php on line 3 Quote Link to comment Share on other sites More sharing options...
resago Posted January 21, 2008 Share Posted January 21, 2008 if (preg_match("/.*.*/", "$string")) { Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 21, 2008 Author Share Posted January 21, 2008 ok new error C:\php>php.exe c:\inetpub\wwwroot\test.php PHP Warning: preg_match(): Compilation failed: unmatched parentheses at offset 3 in on line 3 Quote Link to comment Share on other sites More sharing options...
resago Posted January 21, 2008 Share Posted January 21, 2008 sorry, forgot to escape the ) in the smiley face, put a \ infront of the ) in the smile. Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 21, 2008 Author Share Posted January 21, 2008 Thankyou thankyou thats A Match cheers, Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 21, 2008 Author Share Posted January 21, 2008 re-opened this topic for one last question, im unable to get it to work using a ? could someone also explain what all the / and * are all about? its usually easier to write code when u understand it Quote Link to comment Share on other sites More sharing options...
corbin Posted January 21, 2008 Share Posted January 21, 2008 You could do something like: if(preg_match('/:)/', $m)) { $smiley_count = count($m); //do sql query } Although, you might be better of with counting an explode array since regular expressions are slow..... Or, you could make a function that counts the occurances using strpos and offsets. (PS..... This is extremely random, but I know the DA IRC network has a stat page that shows how many times a certain smiley was done.... You don't happen to use that IRC server do you?) Edit: Bah sorry didn't see the second page x.x. // are simply enclosing the pattern The pattern can start with a bunch of different delimiters as long as they're the same character. Examples: |(.*)| /(.*)/ ~(.*)~ The * simply means any number..... In other words /([a]*)/ would mean 0 or more a's. Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 21, 2008 Author Share Posted January 21, 2008 so what about getting it to work using a ? (question mark) preg_match('/?/', $string); that there doesn't work, and i've tried a few different things, but nothing seems to work Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 21, 2008 Share Posted January 21, 2008 You may want to escape that ? character. preg_match("/\?/", $string); Quote Link to comment Share on other sites More sharing options...
Kingy Posted January 21, 2008 Author Share Posted January 21, 2008 lol even more slashes works now thankyou dude Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 21, 2008 Share Posted January 21, 2008 If you're just matching strings, substr_count() might be more appropriate. It would remove the need for the regular expressions. Quote Link to comment Share on other sites More sharing options...
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.