Jump to content


Photo

implode and exsplode need more examples please


  • Please log in to reply
1 reply to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 31 March 2006 - 03:26 AM

I am trying to get to grips with implode and exsplode but need more proper examples and exsplenations thank you..

Is all this string miniplnation?

example implode

I understand this the implode gets the string and then adds the and.
<?
$num=array("one","two","three","four");
$result=implode(" and " , $num);
echo $result;
?>


exsplode example
quit dont get to grips with this the explode gets the string the with
any space out puts the result with a for loop.

do you need always to use for loops on explode?
<?
$sentence="my little cat";
$result=explode(" " , $sentence);
for($i=0; $i< count($result); $i++) {
echo " number is ".$i." and ".$result[$i]."<br>";
}
?>


thank you for your support.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 March 2006 - 09:13 AM

Implode syntax:
string implode ( string glue, array pieces )

the glue bit is the sting or range of characters that gets inserted between values within an array. which is the pieces part. So for example. If you have any array that stores names or words and yiu want an easy way of displaying this arrat you'll want to use implode. Now you want to display your name or words seperated by a coma. This is how you'll do it:
<?php

$num = array("one", "two", "three"," four");
$result = implode(", " , $num);
echo $result;

?>
That will will result in this: one, two, three, four



Explode syntax:
array explode ( string separator, string string [, int limit] )

Explode is alittle diferent to implode as Explode custs up strings, depending on the seperator specified and puts each cut section into an array. So take this for example:
So you have string like so:
hello I am redarrow and you want each word to be put into an array you'll soiemthing like this:
<?php

$string = "Hello I am redarrow";
$words = explode(" ", $string);

?>
You'll now get an any like so:
array("Hello", "I", "am", "redarrow")

You dont have to use a for loop no. As you can echo out each word sperately like so:
echo $words[0];
echo $words[1];
//ect.

Thats all there is to it. To get a better explanation always check the PHP manul for thse two functions
[a href=\"http://www.php.net/implode\" target=\"_blank\"]implode[/a]
[a href=\"http://www.php.net/explode\" target=\"_blank\"]explode[/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users