Uutiset:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu
Advertisement:

need help for a small code

Aloittaja Shonick, tammikuu 26, 2007, 11:24:55 AP

« edellinen - seuraava »

Shonick

I want to create a small program like this.
  For example, I have this text
Lainaahgaj  jksahf jhsa  uiwyfui  usahf yhwuaf usyfi  jshaf
when the program run, I put this text in to the box. After my program run, I get this text
Lainaahgaj,  jksahf, jhsa, uiwyfui, usahf,yhwuaf, usyfi ,jshaf

What did the program do?
Just put a"," between the words.

anyone can help me witht his code.I reallly need it for my project.

  (optional)second, this code can do is check the words.If any word is repeated then delete that word,just keeps the original word.

  This is so great if you can help me with this code.thanks

arod

to insert a comma between each 2 words, you can do one of several things:
1) if you know for a fact that the words are delimited with a space, just replace the spaces with commas:
Koodi (php) [Valitse]
<?php 
$result
=str_replace(" "","$original_string); 
?>

2) if it can be any number of white spaces (space, tab, newline), you still may be able to use a simple regex replacement:
Koodi (php) [Valitse]
<?php 
$result
=preg_replace('/\s+/'','$original_string); 
?>

if you want to break the string to words, eliminate duplicates, and create a comma delimited string made of the unique words, you'll need a bit of code:
Koodi (php) [Valitse]
<?php
$array 
preg_split('/\s+/'$original_string);
$unique = array();
foreach(
$array as $word)
    
$unique[$word] = 1
$result implode(","array_keys($unique));
?>



Shonick

#2
thanks.this is so great

Advertisement: