Jump to content

[SOLVED] replace colon


Kingy

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

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.